From 8b34127889d7f5dacafc6718514d1713400439ba Mon Sep 17 00:00:00 2001 From: Tanya Sreenagesh Date: Thu, 16 Oct 2025 10:26:13 -0700 Subject: [PATCH] Submission --- MatrixDiagonalOrder.py | 49 ++++++++++++++++++++++++++++++++++++++++++ MatrixSpiralOrder.py | 43 ++++++++++++++++++++++++++++++++++++ ProductExceptSelf.py | 28 ++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 MatrixDiagonalOrder.py create mode 100644 MatrixSpiralOrder.py create mode 100644 ProductExceptSelf.py diff --git a/MatrixDiagonalOrder.py b/MatrixDiagonalOrder.py new file mode 100644 index 00000000..afad11db --- /dev/null +++ b/MatrixDiagonalOrder.py @@ -0,0 +1,49 @@ +# Time Complexity : O(m*n) where m is the number of rows and n is the number of columns +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +My approach: +If the diagonal is going up, move the pointers up and to the right. +If the diagonal is going down, move the pointers down and to the left. +Check for edge cases where the pointers reach the boundaries of the matrix. +''' +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m = len(mat) + n = len(mat[0]) + + result = [] + + i, j = 0, 0 + + up = True + for _ in range(m*n): + result.append(mat[i][j]) + + if up: + if j == n-1: + i+=1 + up=False + elif i == 0: + j+=1 + up=False + else: + i-=1 + j+=1 + + # Down + else: + if i == m-1: + j+=1 + up=True + elif j == 0: + i+=1 + up=True + else: + i+=1 + j-=1 + + return result + \ No newline at end of file diff --git a/MatrixSpiralOrder.py b/MatrixSpiralOrder.py new file mode 100644 index 00000000..56c465c9 --- /dev/null +++ b/MatrixSpiralOrder.py @@ -0,0 +1,43 @@ +# Time Complexity : O(m*n) where m is the number of rows and n is the number of columns +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +My approach: +Use four pointers to keep track of the top, bottom, left, and right boundaries of the matrix. +Iterate through the matrix in a spiral order by moving the pointers after each side is processed. +Sides are always processed in the same order: top, right, bottom, left. +''' +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + top, left = 0, 0 + right = len(matrix[0])-1 + bottom = len(matrix)-1 + + result = [] + + while top <= bottom and left <= right: + # Left to right + for i in range(left, right+1): + result.append(matrix[top][i]) + top+=1 + + # Top to bottom + for j in range(top, bottom+1): + result.append(matrix[j][right]) + right -=1 + + # Right to left + if top <= bottom: + for i in range(right, left-1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + # Bottom to top + if left <= right: + for j in range(bottom, top-1, -1): + result.append(matrix[j][left]) + left+=1 + + return result diff --git a/ProductExceptSelf.py b/ProductExceptSelf.py new file mode 100644 index 00000000..6436411d --- /dev/null +++ b/ProductExceptSelf.py @@ -0,0 +1,28 @@ +# Time Complexity : O(n) where n is the length of the strings +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +''' +My approach: +Calculate the left product for each element, then multiply it with the right running product for each element. +''' +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + + # Starting with 0s to clarify setting first element to 1 + leftProduct = [0] * n + leftProduct[0] = 1 + + # Calculate left running products + for i in range(1, len(nums)): + leftProduct[i] = leftProduct[i-1] * nums[i-1] + + # Keep running counter of right product and multiply with left + rProduct = nums[len(nums)-1] + for i in range(len(nums)-2, -1, -1): + leftProduct[i] = rProduct * leftProduct[i] + rProduct = rProduct * nums[i] + + return leftProduct