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
52 changes: 52 additions & 0 deletions diagonal.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions product_except_self.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions spiral.py
Original file line number Diff line number Diff line change
@@ -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 <right and top < bottom):
break

#every bottom row
for i in range(right-1,left-1,-1):
res.append(matrix[bottom-1][i])
bottom -=1

#every left col
for i in range(bottom-1, top-1,-1):
res.append(matrix[i][left])
left +=1

return res