Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -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

39 changes: 39 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -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)<count:
for i in range(left, right + 1):
result.append(matrix[top][i])
top=top+1
for i in range(top, down + 1):
result.append(matrix[i][right])
right=right-1
if top <= down:
for i in range(right, left - 1, -1):
result.append(matrix[down][i])
down=down-1
if left <= right:
for i in range(down, top - 1, -1):
result.append(matrix[i][left])
left=left+1
return result