From 766ba84e9dc9be54a33098747e303defdf9e6150 Mon Sep 17 00:00:00 2001 From: Ravi Thakur <43831111+ravithakur2k@users.noreply.github.com> Date: Sat, 6 Sep 2025 13:56:22 -0400 Subject: [PATCH] Add matrix traversal and product algorithms Added three new Python files implementing algorithms for diagonal matrix traversal, spiral matrix traversal, and product of array except self. These solutions are optimized for time and space complexity and include comments for clarity. --- DiagonalMatrix.py | 36 ++++++++++++++++++++++++++++++++++++ ProductOfArray.py | 19 +++++++++++++++++++ SpiralMatrix.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 DiagonalMatrix.py create mode 100644 ProductOfArray.py create mode 100644 SpiralMatrix.py diff --git a/DiagonalMatrix.py b/DiagonalMatrix.py new file mode 100644 index 00000000..3f573aff --- /dev/null +++ b/DiagonalMatrix.py @@ -0,0 +1,36 @@ +# Time complexity: O(n) +# Space complexity: O(1) since we are not considering the output as an extra space as it is auxiliary space. +# Did it run on leetcode: Yes + +# Basically checking for out of bounds condition and updating the direction based on it and storing the result in result array + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m, n = len(mat), len(mat[0]) + result = [0 for _ in range(m*n)] + dir = True + r,c = 0, 0 + c = 0 + for i in range(m*n): + result[i] = mat[r][c] + if dir: + if(r ==0 and c != n - 1): + c += 1 + dir = False + elif( c == n - 1): + r += 1 + dir = False + else: + r -= 1 + c += 1 + else: + if(c==0 and r != m-1): + r += 1 + dir = True + elif(r == m - 1): + c += 1 + dir = True + else: + c -= 1 + r += 1 + return result \ No newline at end of file diff --git a/ProductOfArray.py b/ProductOfArray.py new file mode 100644 index 00000000..1b02ad96 --- /dev/null +++ b/ProductOfArray.py @@ -0,0 +1,19 @@ +# Time complexity: O(n) +# Space complexity: O(1) since we are not considering the output as an extra space as it is auxiliary space. +# Did it run on leetcode: Yes + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + res = [1] * len(nums) + lp = 1 # left running product + for i in range(1, len(nums)): + lp = lp * nums[i - 1] + res[i] = lp + + rp = 1 # right running product + + for i in range(len(nums) - 2, -1, -1): + rp = rp * nums[i + 1] + res[i] *= rp + + return res \ No newline at end of file diff --git a/SpiralMatrix.py b/SpiralMatrix.py new file mode 100644 index 00000000..ff08e2e0 --- /dev/null +++ b/SpiralMatrix.py @@ -0,0 +1,33 @@ +# Time complexity: O(m*n) +# Space complexity: O(1) since we are not considering the output as an extra space as it is auxiliary space. +# Did it run on leetcode: Yes + +# We are checking for top <= bottom and left <= right only for 2 loops because for others the for loops take care + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m, n = len(matrix), len(matrix[0]) + result = [] + top, left = 0, 0 + right = n - 1 + bottom = m - 1 + while (left <= right and top <= bottom): + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + for j in range(top, bottom + 1): + result.append(matrix[j][right]) + right -= 1 + + if (top <= bottom): + for k in range(right, left - 1, -1): + result.append(matrix[bottom][k]) + bottom -= 1 + + if (left <= right): + for l in range(bottom, top - 1, -1): + result.append(matrix[l][left]) + left += 1 + + return result \ No newline at end of file