From 16292c8b201f96e933e14e573eede3499745e166 Mon Sep 17 00:00:00 2001 From: ashritha0806 <111675125+ashritha0806@users.noreply.github.com> Date: Mon, 27 Apr 2026 15:51:29 -0700 Subject: [PATCH] Done Array-1 --- diagonal.py | 52 ++++++++++++++++++++++++++++++++++++++++++ product_except_self.py | 32 ++++++++++++++++++++++++++ spiral.py | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 diagonal.py create mode 100644 product_except_self.py create mode 100644 spiral.py diff --git a/diagonal.py b/diagonal.py new file mode 100644 index 00000000..81cca70e --- /dev/null +++ b/diagonal.py @@ -0,0 +1,52 @@ +# Time Complexity : O(m*n) +# Space complexity :O(1) auxiliary space +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# using a boolean flag to track whether the current movement is diagonally upward or downward. +# In upward movement steps up-right but if it hits the top edge it shifts right, or if it hits the right edge it shifts down, and flips the direction flag to downward. +# In downward movement steps down-left but if it hits the left edge it shifts down, or if it hits the bottom edge it shifts right, and flips the direction flag back to upward. + +class Solution(object): + def findDiagonalOrder(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + m = len(mat) + n = len(mat[0]) + #True -> moving up + #False ->moving down + direction = True + output = [0] * m * n + r,c = 0,0 + + for i in range(0,len(output)): + output[i] = mat[r][c] + #moving up + if direction == True: + if r == 0 and c != n-1: + c += 1 + direction = False + #right edge + elif c == n-1: + r += 1 + direction = False + else: + r -= 1 + c += 1 + # moving down + else: + + if c == 0 and r != m -1: + r += 1 + direction = True + #left edge + elif r == m-1: + c += 1 + direction = True + else: + c -= 1 + r += 1 + return output \ No newline at end of file diff --git a/product_except_self.py b/product_except_self.py new file mode 100644 index 00000000..baef35e4 --- /dev/null +++ b/product_except_self.py @@ -0,0 +1,32 @@ +# Time Complexity : O(n) +# Space complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : It was hard to come up with prefix and suffix product solution. + +# Your code here along with comments explaining your approach +# Build the result array by calculating the cumulative product of everything to the left of each index, starting with a base of 1 because the first element has no left neighbors. +# Loop backward and keep a running product of everything to the right, multiplying suffix directly into the prefix values in the result array. + +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + + rp = 1 + n = len(nums) + result = [0] * n + + #forward pass left side product + result[0] = 1 + for i in range(1,n): + rp = rp * nums[i-1] + result[i] = rp + + #backward pass ride product + rp = 1 + for i in range(n-2, -1, -1): + rp = rp * nums[i+1] + result[i] = result[i] * rp + return result diff --git a/spiral.py b/spiral.py new file mode 100644 index 00000000..402bbea2 --- /dev/null +++ b/spiral.py @@ -0,0 +1,45 @@ +# Time Complexity : O(m* n) +# Space complexity :O(1) - Auxiliary space +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# Using 4 pointers to keep track of the top, bottom, left and right of the matrix. +# After completing each side of a layer,will update the boundary pointer to inwards and stop when the boundareis cross(left > right or top > bottom). + + +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + top,bottom = 0, len(matrix) + left, right = 0, len(matrix[0]) + res = [] + while left < right and top < bottom: + #every top row + for i in range(left,right): + res.append(matrix[top][i]) + top += 1 + + #every right col + for i in range(top, bottom): + res.append(matrix[i][right-1]) + right -= 1 + + #need if not square matrix + if not (left