From 2cbfd2339e59b3d3e54b77eb8b60d124fb0137f6 Mon Sep 17 00:00:00 2001 From: sainaga00 Date: Fri, 20 Feb 2026 16:01:26 -0500 Subject: [PATCH] Array --- diagMatrix.py | 42 ++++++++++++++++++++++++++++++++++++++++++ productArr.py | 17 +++++++++++++++++ spiralMatrix.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 diagMatrix.py create mode 100644 productArr.py create mode 100644 spiralMatrix.py diff --git a/diagMatrix.py b/diagMatrix.py new file mode 100644 index 00000000..cee20f33 --- /dev/null +++ b/diagMatrix.py @@ -0,0 +1,42 @@ +# Time Complexity : O(n × m) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: Traverse the matrix diagonally by alternating directions. +# When a boundary is hit, change direction and move to the next valid start cell. +# Continue this process until all elements are visited. + + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + dir = True + n = len(mat) + m = len(mat[0]) + res = [0] * (m * n) + r,c = 0,0 + + for i in range(m * n): + res[i] = mat[r][c] + if dir: + if c == m - 1: + dir = False + r += 1 + elif r == 0: + dir = False + c += 1 + else: + r -= 1 + c += 1 + else: + if r == n - 1: + dir = True + c += 1 + elif c == 0: + dir = True + r += 1 + else: + r += 1 + c -= 1 + + return res + diff --git a/productArr.py b/productArr.py new file mode 100644 index 00000000..05ca23b7 --- /dev/null +++ b/productArr.py @@ -0,0 +1,17 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: First store prefix products in the result array. +# Then traverse from right while maintaining a suffix product and multiply it with current result. + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + res = [1] * len(nums) + for i in range(1,len(nums)): + res[i] = res[i - 1] * nums[i - 1] + suff = nums[-1] + for i in range(len(nums) - 2,-1,-1): + res[i] *= suff + suff *= nums[i] + return res \ No newline at end of file diff --git a/spiralMatrix.py b/spiralMatrix.py new file mode 100644 index 00000000..50ed4cdd --- /dev/null +++ b/spiralMatrix.py @@ -0,0 +1,39 @@ +# Time Complexity : O(n × m) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: Traverse the matrix in spiral order using four boundaries (top, bottom, left, right). +# Move right → down → left → up and Repeat this process layer by layer until all elements are visited. + + + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + n = len(matrix) + m = len(matrix[0]) + left,top = 0,0 + right = m - 1 + bottom = n - 1 + res = [] + while top <= bottom and left <= right: + + for i in range(left,right + 1): + res.append(matrix[top][i]) + top += 1 + + for i in range(top,bottom + 1): + res.append(matrix[i][right]) + right -= 1 + + if top <= bottom: + for i in range(right,left - 1,-1): + res.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + for i in range(bottom,top - 1,-1): + res.append(matrix[i][left]) + left += 1 + + return res +