From c1b2cee73cebb0eb106e4186feaf269d63a58a08 Mon Sep 17 00:00:00 2001 From: amitmittal117 Date: Tue, 25 Nov 2025 00:28:11 -0800 Subject: [PATCH] [IMP] Array-1 --- diagonal-traverse.py | 43 +++++++++++++++++++++++++++ product-of-array-except-self.py | 51 +++++++++++++++++++++++++++++++++ spiral-matrix.py | 44 ++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 diagonal-traverse.py create mode 100644 product-of-array-except-self.py create mode 100644 spiral-matrix.py diff --git a/diagonal-traverse.py b/diagonal-traverse.py new file mode 100644 index 00000000..a5df7582 --- /dev/null +++ b/diagonal-traverse.py @@ -0,0 +1,43 @@ +class Solution: + def findDiagonalOrder(self, mat): + ''' + TC: O(n) + SC: O(1) + Leetcode: Solved without any issue. + issue: we can use a while loop instead of a for loop. + ''' + m = len(mat) + n = len(mat[0]) + r, c = 0, 0 + flag = True + res = [] + for i in range(m * n): + res.append(mat[r][c]) + if flag: + if r == 0 and c != n - 1: + c += 1 + flag = False + elif c == n - 1: + r += 1 + flag = False + else: + r -= 1 + c += 1 + else: + if c == 0 and r != m - 1: + r += 1 + flag = True + elif r == m - 1: + c += 1 + flag = True + else: + c -= 1 + r += 1 + return res + + +s = Solution() +mat = [[1,2,3],[4,5,6],[7,8,9]] +print(s.findDiagonalOrder(mat)) +mat = [[1,2],[3,4]] +print(s.findDiagonalOrder(mat)) diff --git a/product-of-array-except-self.py b/product-of-array-except-self.py new file mode 100644 index 00000000..4f12548a --- /dev/null +++ b/product-of-array-except-self.py @@ -0,0 +1,51 @@ +class Solution: + ''' + TC: O(3n) = O(n) + SC: O(2n) = O(n) + Leetcode: solved this problem + Issue: here we are using more space then required we can optimize this. + ''' + def productExceptSelf(self, nums): + res = [] + rp = 1 + n = len(nums) + left = [1] * n + right = [1] * n + for i in range(1, n): + rp *= nums[i - 1] + left[i] = rp + rp = 1 + for i in range(n - 2, -1, -1): + rp *= nums[i + 1] + right[i] = rp + for i in range(n): + res.append(left[i] * right[i]) + return res + + ''' + TC: O(2n) = O(n) + SC: O(1) + Leetcode: Solve this problem + Issue: Optimized with no issue. + ''' + def productExceptSelf(self, nums): + n = len(nums) + res = [1] * n + rp = 1 + for i in range(1, n): + rp *= nums[i - 1] + res[i] = rp + rp = 1 + for i in range(n - 2, -1, -1): + rp *= nums[i + 1] + res[i] *= rp + + return res + + + +s = Solution() +nums = [1,2,3,4] +print(s.productExceptSelf(nums)) +nums = [-1,1,0,-3,3] +print(s.productExceptSelf(nums)) \ No newline at end of file diff --git a/spiral-matrix.py b/spiral-matrix.py new file mode 100644 index 00000000..032e71b7 --- /dev/null +++ b/spiral-matrix.py @@ -0,0 +1,44 @@ +class Solution: + ''' + TC: O(m *n) + SC: O(1) + Leetcode: Completed the problem with no issue. + intuation: Use 4 pointer top, left, right, and bottom. use them to traverse the matrix to get the spiral. + ''' + def spiralOrder(self, matrix): + m = len(matrix) + n = len(matrix[0]) + left = 0 + right = n - 1 + top = 0 + bottom = m - 1 + res = [] + while left <= right and top <= bottom: + # top + for i in range(left, right + 1): + res.append(matrix[top][i]) + top += 1 + + # right + for i in range(top, bottom + 1): + res.append(matrix[i][right]) + right -= 1 + + # bottom + if top <= bottom: + for i in range(right, left-1, -1): + res.append(matrix[bottom][i]) + bottom -= 1 + # left + if left <= right: + for i in range(bottom, top-1, -1): + res.append(matrix[i][left]) + left += 1 + return res + + +s = Solution() +matrix = [[1,2,3],[4,5,6],[7,8,9]] +print(s.spiralOrder(matrix)) +matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] +print(s.spiralOrder(matrix))