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
49 changes: 49 additions & 0 deletions MatrixDiagonalOrder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Time Complexity : O(m*n) where m is the number of rows and n is the number of columns
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

'''
My approach:
If the diagonal is going up, move the pointers up and to the right.
If the diagonal is going down, move the pointers down and to the left.
Check for edge cases where the pointers reach the boundaries of the matrix.
'''
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m = len(mat)
n = len(mat[0])

result = []

i, j = 0, 0

up = True
for _ in range(m*n):
result.append(mat[i][j])

if up:
if j == n-1:
i+=1
up=False
elif i == 0:
j+=1
up=False
else:
i-=1
j+=1

# Down
else:
if i == m-1:
j+=1
up=True
elif j == 0:
i+=1
up=True
else:
i+=1
j-=1

return result

43 changes: 43 additions & 0 deletions MatrixSpiralOrder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Time Complexity : O(m*n) where m is the number of rows and n is the number of columns
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

'''
My approach:
Use four pointers to keep track of the top, bottom, left, and right boundaries of the matrix.
Iterate through the matrix in a spiral order by moving the pointers after each side is processed.
Sides are always processed in the same order: top, right, bottom, left.
'''
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
top, left = 0, 0
right = len(matrix[0])-1
bottom = len(matrix)-1

result = []

while top <= bottom and left <= right:
# Left to right
for i in range(left, right+1):
result.append(matrix[top][i])
top+=1

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

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

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

return result
28 changes: 28 additions & 0 deletions ProductExceptSelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Time Complexity : O(n) where n is the length of the strings
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

'''
My approach:
Calculate the left product for each element, then multiply it with the right running product for each element.
'''
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)

# Starting with 0s to clarify setting first element to 1
leftProduct = [0] * n
leftProduct[0] = 1

# Calculate left running products
for i in range(1, len(nums)):
leftProduct[i] = leftProduct[i-1] * nums[i-1]

# Keep running counter of right product and multiply with left
rProduct = nums[len(nums)-1]
for i in range(len(nums)-2, -1, -1):
leftProduct[i] = rProduct * leftProduct[i]
rProduct = rProduct * nums[i]

return leftProduct