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
42 changes: 42 additions & 0 deletions diagMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Time Complexity : O(n × m)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Traverse the matrix diagonally by alternating directions.
# When a boundary is hit, change direction and move to the next valid start cell.
# Continue this process until all elements are visited.


class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
dir = True
n = len(mat)
m = len(mat[0])
res = [0] * (m * n)
r,c = 0,0

for i in range(m * n):
res[i] = mat[r][c]
if dir:
if c == m - 1:
dir = False
r += 1
elif r == 0:
dir = False
c += 1
else:
r -= 1
c += 1
else:
if r == n - 1:
dir = True
c += 1
elif c == 0:
dir = True
r += 1
else:
r += 1
c -= 1

return res

17 changes: 17 additions & 0 deletions productArr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 store prefix products in the result array.
# Then traverse from right while maintaining a suffix product and multiply it with current result.

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1] * len(nums)
for i in range(1,len(nums)):
res[i] = res[i - 1] * nums[i - 1]
suff = nums[-1]
for i in range(len(nums) - 2,-1,-1):
res[i] *= suff
suff *= nums[i]
return res
39 changes: 39 additions & 0 deletions spiralMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity : O(n × m)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Traverse the matrix in spiral order using four boundaries (top, bottom, left, right).
# Move right → down → left → up and Repeat this process layer by layer until all elements are visited.



class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
n = len(matrix)
m = len(matrix[0])
left,top = 0,0
right = m - 1
bottom = n - 1
res = []
while top <= bottom and left <= right:

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

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

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

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

return res