From 1965a30b0a57f2f1e030be013112e6f26dd3d1a6 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Tue, 4 Jan 2022 18:02:21 -0500 Subject: [PATCH 1/9] Create Diagonal_order.py --- Diagonal_order.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Diagonal_order.py diff --git a/Diagonal_order.py b/Diagonal_order.py new file mode 100644 index 00000000..d74968a9 --- /dev/null +++ b/Diagonal_order.py @@ -0,0 +1,37 @@ +# Time Complexity - O(mn) +# Space Complexity - O(1) + +class Solution: + + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + direc = 1 + r = 0 + c = 0 + m = len(mat) + n = len(mat[0]) + result = [None]*m*n + i=0 + while i Date: Tue, 4 Jan 2022 18:36:23 -0500 Subject: [PATCH 2/9] Create spiral_order.py --- spiral_order.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spiral_order.py diff --git a/spiral_order.py b/spiral_order.py new file mode 100644 index 00000000..7386ad87 --- /dev/null +++ b/spiral_order.py @@ -0,0 +1,37 @@ +# Time Complexity - O(mn) +# Space Complexity - O(1) + +class Solution: + + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m = len(matrix) + n = len(matrix[0]) + result = [] + top = 0 + bottom = m-1 + left = 0 + right = n-1 + i = 0 + while left<=right and top<=bottom: + # top row + for i in range(left,right+1): + result.append(matrix[top][i]) + top = top+1 + + # right column + for j in range(top,bottom+1): + result.append(matrix[j][right]) + right = right-1 + + # bottom row + if top<=bottom: + for i in range(right,left-1,-1): + result.append(matrix[bottom][i]) + bottom = bottom-1 + + # left column + if left<=right: + for j in range(bottom,top-1,-1): + result.append(matrix[j][left]) + left = left+1 + return result From ca6a77f55ecf181f3588d1354b38bf4b75c35b26 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Wed, 5 Jan 2022 12:29:20 -0500 Subject: [PATCH 3/9] Create Product_of_array_except_self.py --- Product_of_array_except_self.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Product_of_array_except_self.py diff --git a/Product_of_array_except_self.py b/Product_of_array_except_self.py new file mode 100644 index 00000000..e0cbe9dd --- /dev/null +++ b/Product_of_array_except_self.py @@ -0,0 +1,17 @@ +# Time Complexity - O(n) +# Space Complexity - O(1) + +class Solution: + + def productExceptSelf(self, nums: List[int]) -> List[int]: + result = [None]*len(nums) + rprod = 1 + result[0] = rprod + for i in range(1,len(nums)): + rprod = rprod*nums[i-1] + result[i] = rprod + rprod = 1 + for i in range(len(nums)-2,-1,-1): + rprod = rprod*nums[i+1] + result[i] = rprod*result[i] + return result From e42ecbe6e2f462853018b2749f242a9c7a4add19 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 25 Apr 2026 19:03:09 -0700 Subject: [PATCH 4/9] Delete Diagonal_order.py --- Diagonal_order.py | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 Diagonal_order.py diff --git a/Diagonal_order.py b/Diagonal_order.py deleted file mode 100644 index d74968a9..00000000 --- a/Diagonal_order.py +++ /dev/null @@ -1,37 +0,0 @@ -# Time Complexity - O(mn) -# Space Complexity - O(1) - -class Solution: - - def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: - direc = 1 - r = 0 - c = 0 - m = len(mat) - n = len(mat[0]) - result = [None]*m*n - i=0 - while i Date: Sat, 25 Apr 2026 19:03:18 -0700 Subject: [PATCH 5/9] Delete Product_of_array_except_self.py --- Product_of_array_except_self.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 Product_of_array_except_self.py diff --git a/Product_of_array_except_self.py b/Product_of_array_except_self.py deleted file mode 100644 index e0cbe9dd..00000000 --- a/Product_of_array_except_self.py +++ /dev/null @@ -1,17 +0,0 @@ -# Time Complexity - O(n) -# Space Complexity - O(1) - -class Solution: - - def productExceptSelf(self, nums: List[int]) -> List[int]: - result = [None]*len(nums) - rprod = 1 - result[0] = rprod - for i in range(1,len(nums)): - rprod = rprod*nums[i-1] - result[i] = rprod - rprod = 1 - for i in range(len(nums)-2,-1,-1): - rprod = rprod*nums[i+1] - result[i] = rprod*result[i] - return result From 11a8b44183d6c06e85c80ca34830ee1a745ab3ff Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 25 Apr 2026 19:03:26 -0700 Subject: [PATCH 6/9] Delete spiral_order.py --- spiral_order.py | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 spiral_order.py diff --git a/spiral_order.py b/spiral_order.py deleted file mode 100644 index 7386ad87..00000000 --- a/spiral_order.py +++ /dev/null @@ -1,37 +0,0 @@ -# Time Complexity - O(mn) -# Space Complexity - O(1) - -class Solution: - - def spiralOrder(self, matrix: List[List[int]]) -> List[int]: - m = len(matrix) - n = len(matrix[0]) - result = [] - top = 0 - bottom = m-1 - left = 0 - right = n-1 - i = 0 - while left<=right and top<=bottom: - # top row - for i in range(left,right+1): - result.append(matrix[top][i]) - top = top+1 - - # right column - for j in range(top,bottom+1): - result.append(matrix[j][right]) - right = right-1 - - # bottom row - if top<=bottom: - for i in range(right,left-1,-1): - result.append(matrix[bottom][i]) - bottom = bottom-1 - - # left column - if left<=right: - for j in range(bottom,top-1,-1): - result.append(matrix[j][left]) - left = left+1 - return result From 6865cc23ca600d17a7ca381af299874dfb6d8c50 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 25 Apr 2026 19:36:46 -0700 Subject: [PATCH 7/9] Add productExceptSelf function with two approaches Implemented two approaches for the product of array except self, with O(n) and O(n^2) time complexities. --- ProductOfArrayExceptSelf.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ProductOfArrayExceptSelf.py diff --git a/ProductOfArrayExceptSelf.py b/ProductOfArrayExceptSelf.py new file mode 100644 index 00000000..b532a317 --- /dev/null +++ b/ProductOfArrayExceptSelf.py @@ -0,0 +1,35 @@ +# Time Complexity --> O(n) +# Space Complexity --> O(1) There is no auxillary space +# Approach --> At each index, we find the running product to the left and right of it individually and then produce the result using their product. +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [] + # Find the running prod to the left of each element + lprod = 1 + answer.append(lprod) + for i in range(1, len(nums)): + lprod = lprod * nums[i-1] + answer.append(lprod) + + rprod = 1 + for i in range(len(nums)-2,-1,-1): + rprod = rprod * nums[i+1] + answer[i] = answer[i]*rprod + return answer + + +''' +# Time Complexity --> O(n*2) +# Space Complexity --> O(1) There is no auxillary space +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [] + for i in range(len(nums)): + prod = 1 + for j in range(len(nums)): + if i!=j: + prod = prod*nums[j] + answer.append(prod) + return answer + +''' From 29d17f8b4f89b1fa6a3bc7552b3ce204263d5cd0 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 25 Apr 2026 20:34:27 -0700 Subject: [PATCH 8/9] Create DiagonalTraversal.py --- DiagonalTraversal.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 DiagonalTraversal.py diff --git a/DiagonalTraversal.py b/DiagonalTraversal.py new file mode 100644 index 00000000..412a81d9 --- /dev/null +++ b/DiagonalTraversal.py @@ -0,0 +1,36 @@ +# Time Complexity --> O(m*n) +# Space Complexity --> O(1). There is no auxillary space +# Approach --> We traverse through the matrix in such a way that at each boundary, we create rules that change the direction and the way we tranverse to next within bounds element +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m = len(mat) + n = len(mat[0]) + + i = 0 + j = 0 + re = [0]*(m*n) + upflag = 1 + for k in range(m*n): + re[k] = mat[i][j] + if upflag==1: + if j==n-1: + i = i+1 + upflag = 0 + elif i==0: + j = j+1 + upflag = 0 + else: + i = i-1 + j = j+1 + else: + if i==m-1: + j = j+1 + upflag = 1 + elif j==0: + i = i+1 + upflag = 1 + else: + i = i+1 + j = j-1 + return re + From 02dc7cca64a777ff5e6e6585fdf6e09c917d56e1 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 25 Apr 2026 21:14:07 -0700 Subject: [PATCH 9/9] Create SpiralMatrix.py --- SpiralMatrix.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SpiralMatrix.py diff --git a/SpiralMatrix.py b/SpiralMatrix.py new file mode 100644 index 00000000..0cb86106 --- /dev/null +++ b/SpiralMatrix.py @@ -0,0 +1,35 @@ +# Time Complexity --> O(m*n) +# Space Complexity --> O(1) as there is no auxillary space utilized +# Approach --> We start off with boundaries on 4 sides of the matrix and as we traverse through a row or a column, we manipulate the boundary values to avoid traversing the same elements. +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m = len(matrix) + n = len(matrix[0]) + top, bottom = 0, m-1 + left, right = 0, n-1 + + re = [] + while top<=bottom and left<=right: + # left to right + for j in range(left, right+1): + re.append(matrix[top][j]) + top = top+1 + + # top to bottom + for i in range(top, bottom+1): + re.append(matrix[i][right]) + right = right-1 + + if top<=bottom: + # right to left + for j in range(right, left-1, -1): + re.append(matrix[bottom][j]) + bottom = bottom-1 + + if left<=right: + # bottom to top + for i in range(bottom, top-1, -1): + re.append(matrix[i][left]) + left = left+1 + + return re