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
33 changes: 33 additions & 0 deletions diagonal_traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: #Tc: O(m*n) SC: O(1)
m=len(mat)
n=len(mat[0])
result=[0]*(m*n)
r=c=0
dir= True
for i in range(m*n):
result[i]=mat[r][c]
#upward
if dir:
if c==n-1:
r+=1
dir=False
elif r==0:
c+=1
dir=False
else:
r-=1
c+=1

#downward
else:
if r==m-1:
c+=1
dir=True
elif c==0:
r+=1
dir=True
else:
r+=1
c-=1
return result

15 changes: 15 additions & 0 deletions product_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def productExceptSelf(self, nums: List[int]) -> List[int]: # TC: O(n) SC: O(1)
n=len(nums)
result=[0]*n
result[0]=1
left=1
for i in range(1,n):
left=left*nums[i-1]
result[i]=left

right=1
for i in range(n-2,-1,-1):
right=right*nums[i+1]
result[i]=result[i]*right
return result

35 changes: 35 additions & 0 deletions spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def spiralOrder(self, matrix: List[List[int]]) -> List[int]: #TC: O(m*n) SC:O(1)
m=len(matrix)
n=len(matrix[0])
r=c=0
result=[]
# self.helper(matrix,0,0,n-1,m-1)

top=left=0
bottom=m-1
right=n-1
# def helper(self, matrix, left, top, right, bottom):
while top <= bottom and left <= right:

# top row
for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1

# right column
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1

if top<=bottom:
# bottom row
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1

if left <= right:
# left column
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
return result