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
40 changes: 40 additions & 0 deletions diagonal-traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
''' 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

Approach : If we reach top or right boundary: flip the direction to downward
if at left and bottom boundary flip the direction to upward
handle the edge case at corners
'''
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
rows=len(mat)
cols=len(mat[0])
result=[]
flag=True
r,c = 0, 0
for i in range(rows*cols):
result.append(mat[r][c])
if flag:
if c == cols-1:
r += 1
flag=False
elif r == 0:
c += 1
flag =False
else:
r -= 1
c += 1
else:
if r == rows-1:
c += 1
flag=True
elif c == 0:
r += 1
flag =True
else:
r += 1
c -= 1

return result
22 changes: 22 additions & 0 deletions product-of-array-except-self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
''' 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 do left pass to calculate running product.
Then second iteration from right to left and updating the new product
'''

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n =len(nums)
L = [1] * n
prev, post = 1,1
for i in range(n):
L[i] = prev
prev = prev * nums[i]
for j in range(n-1,-1,-1):
L[j] = post * L[j]
post = post * nums[j]

return L
73 changes: 73 additions & 0 deletions spiral-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
''' 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

Approach : First do left pass to calculate running product.
Then second iteration from right to left and updating the new product
'''

#---------- Solution 1 : Checking the variable before every for loop iteration--------
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
rows,cols= len(matrix),len(matrix[0])
l, t = 0,0
r, b = cols-1,rows-1
result = []
while l <= r and t <= b:
#for left to right
for i in range(l,r + 1):
result.append(matrix[t][i])
t += 1

if l <= r and t <= b:
#for top to bottom
for i in range(t,b + 1):
result.append(matrix[i][r])
r -= 1

if l <= r and t <= b:
#for right to left
for i in range(r,l-1, -1):
result.append(matrix[b][i])
b -= 1

if l <= r and t <= b:
#for bottom to top
for i in range(b,t-1,-1):
result.append(matrix[i][l])
l += 1
return result

#---------- Solution 2 : Optimized condition check before requied for loop iteration--------
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
rows,cols= len(matrix),len(matrix[0])
l, t = 0,0
r, b = cols-1,rows-1
result = []
while l <= r and t <= b:
#for left to right
for i in range(l,r + 1):
result.append(matrix[t][i])
t += 1

#for top to bottom
for i in range(t,b + 1):
result.append(matrix[i][r])
r -= 1

if t <= b:
#for right to left
for i in range(r,l-1, -1):
result.append(matrix[b][i])
b -= 1

if l <= r:
#for bottom to top
for i in range(b,t-1,-1):
result.append(matrix[i][l])
l += 1
return result