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
55 changes: 55 additions & 0 deletions diagonal_traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
rowLen = len(mat)
if rowLen == 0:
return []
colLen = len(mat[0])

res = [] # Result list to store elements in diagonal order
row = 0 # Start at the top-left corner
col = 0
directionUp = True # Tracks the current direction of traversal (up-right or down-left)

# Continue until we’ve processed all elements
while row < rowLen and col < colLen:

if directionUp:
# --- Moving UP-RIGHT direction ---
# Keep going up (row--) and right (col++) while staying within matrix bounds
while row > 0 and col < (colLen - 1):
res.append(mat[row][col])
row -= 1
col += 1

# Append the last element before switching direction
res.append(mat[row][col])

# If we reached the last column, we can only move down to next row
if col == (colLen - 1):
row += 1
else:
# Otherwise, move right to the next column
col += 1

else:
# --- Moving DOWN-LEFT direction ---
# Keep going down (row++) and left (col--) while staying within matrix bounds
while col > 0 and row < (rowLen - 1):
res.append(mat[row][col])
row += 1
col -= 1

# Append the last element before switching direction
res.append(mat[row][col])

# If we reached the last row, we can only move right to next column
if row == (rowLen - 1):
col += 1
else:
# Otherwise, move down to the next row
row += 1

# Flip direction for the next diagonal
directionUp = not directionUp

return res
48 changes: 48 additions & 0 deletions product_array_except_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)

# Step 1: Initialize result array with 1s.
# 'res[i]' will eventually hold the product of all elements
# to the LEFT of index i (initially all 1s as placeholders).
res = [1] * n

# Step 2: Compute prefix products (products of all elements to the left of i)
# Example: nums = [1, 2, 3, 4]
# After this loop, res = [1, 1, 2, 6]
# Explanation:
# res[1] = res[0] * nums[0] = 1 * 1 = 1
# res[2] = res[1] * nums[1] = 1 * 2 = 2
# res[3] = res[2] * nums[2] = 2 * 3 = 6
for i in range(1, n):
res[i] = res[i - 1] * nums[i - 1]

# Step 3: Initialize variable 'product' to hold suffix product
# (product of elements to the right of the current index)
# Start with the last element since nothing is to its right.
product = nums[-1]

# Step 4: Traverse array backward (from second last element to first)
# Multiply each res[i] (which currently has prefix product)
# by 'product' (which represents suffix product).
# Then, update 'product' by multiplying it with nums[i].
#
# Example trace (nums = [1, 2, 3, 4]):
# Initially product = 4
#
# i = 2 → res[2] = res[2] * 4 = 2 * 4 = 8
# product = product * nums[2] = 4 * 3 = 12
#
# i = 1 → res[1] = res[1] * 12 = 1 * 12 = 12
# product = product * nums[1] = 12 * 2 = 24
#
# i = 0 → res[0] = res[0] * 24 = 1 * 24 = 24
# product = product * nums[0] = 24 * 1 = 24
#
# Final res = [24, 12, 8, 6]
for i in range(n - 2, -1, -1):
res[i] = res[i] * product
product = product * nums[i]

# Step 5: Return the final result array
return res
32 changes: 32 additions & 0 deletions spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
rows = len(matrix)
if rows == 0:
return []
cols = len(matrix[0])
return self.helper(matrix, 0, rows - 1, 0, cols - 1)

def helper(self, matrix: List[List[int]], t, b, l, r) -> List[int]:
if t > b or l > r:
return []
# Single cell left
if t == b and l == r:
return [matrix[t][l]]

res = []
# top row
for i in range(l, r + 1):
res.append(matrix[t][i])
# right column
for i in range(t + 1, b + 1):
res.append(matrix[i][r])
# bottom row (if more than one row)
if t < b:
for i in range(r - 1, l - 1, -1):
res.append(matrix[b][i])
# left column (if more than one column)
if l < r:
for i in range(b - 1, t, -1):
res.append(matrix[i][l])

return res + self.helper(matrix, t + 1, b - 1, l + 1, r - 1)