From 9f3441587d11821197cb3fdae579a0c58fcf5e5e Mon Sep 17 00:00:00 2001 From: sreevardhan1099 Date: Mon, 19 Jan 2026 20:56:57 -0500 Subject: [PATCH] Completed Array-1 --- Problem-1.py | 26 ++++++++++++++++++++++++++ Problem-2.py | 41 +++++++++++++++++++++++++++++++++++++++++ Problem-3.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 Problem-1.py create mode 100644 Problem-2.py create mode 100644 Problem-3.py diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 00000000..a528a611 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,26 @@ +# Product Except Self +# Time Complexity :O(n) +# Space Complexity :O(1) +# 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 +#In the result array maintain the left product of each element and iterate backewards to multiple the left product with right product +#time - O(n) +#space - O(1) +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + res=[0]*len(nums) + i=0 + res[i]=1 + lp=1 + for i in range(1,len(nums)): #iterate for left product + lp=lp*nums[i-1] + res[i]=lp + + rp=1 + for j in range(len(nums)-2,-1,-1):#iterate for left product + rp=rp*nums[j+1] + res[j]=rp*res[j] + return res diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 00000000..4adb5859 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,41 @@ +# Diagonal Traverse +# Time Complexity :O(m*n) +# Space Complexity O(1) +# 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 +#start with up direction and if boundaries of the matrix is exceeded then change direction, iterate until the result array is equal to number of rows*columns +# time- O(m*n) +#space - O(1) +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m, n = len(mat), len(mat[0]) + r,c=0,0 + res=[] + dir="up" + while(len(res)<(m*n)): + res.append(mat[r][c]) + if(dir=="up"): + if c==n-1: + r=r+1 + dir="down" + elif r==0: + c=c+1 + dir="down" + else: + r=r-1 + c=c+1 + else: + if r==m-1: + c=c+1 + dir="up" + elif c==0: + r=r+1 + dir="up" + else: + r=r+1 + c=c-1 + return res + \ No newline at end of file diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 00000000..3736e98e --- /dev/null +++ b/Problem-3.py @@ -0,0 +1,39 @@ +# Time Complexity :O(m*n) +# Space Complexity :O(1) +# 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 +# iterate in each direction until we reach the boundary set +#start with right, down, left and up, repeat this until all the elements of matrix are added to result +# top - O(m*n) +# space - O(1) +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m=len(matrix) + n=len(matrix[0]) + count=m*n + top=0 + down=m-1 + left=0 + right = n-1 + dir="right" + result=[] + while len(result)