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
43 changes: 43 additions & 0 deletions diagonal-traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution:
def findDiagonalOrder(self, mat):
'''
TC: O(n)
SC: O(1)
Leetcode: Solved without any issue.
issue: we can use a while loop instead of a for loop.
'''
m = len(mat)
n = len(mat[0])
r, c = 0, 0
flag = True
res = []
for i in range(m * n):
res.append(mat[r][c])
if flag:
if r == 0 and c != n - 1:
c += 1
flag = False
elif c == n - 1:
r += 1
flag = False
else:
r -= 1
c += 1
else:
if c == 0 and r != m - 1:
r += 1
flag = True
elif r == m - 1:
c += 1
flag = True
else:
c -= 1
r += 1
return res


s = Solution()
mat = [[1,2,3],[4,5,6],[7,8,9]]
print(s.findDiagonalOrder(mat))
mat = [[1,2],[3,4]]
print(s.findDiagonalOrder(mat))
51 changes: 51 additions & 0 deletions product-of-array-except-self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution:
'''
TC: O(3n) = O(n)
SC: O(2n) = O(n)
Leetcode: solved this problem
Issue: here we are using more space then required we can optimize this.
'''
def productExceptSelf(self, nums):
res = []
rp = 1
n = len(nums)
left = [1] * n
right = [1] * n
for i in range(1, n):
rp *= nums[i - 1]
left[i] = rp
rp = 1
for i in range(n - 2, -1, -1):
rp *= nums[i + 1]
right[i] = rp
for i in range(n):
res.append(left[i] * right[i])
return res

'''
TC: O(2n) = O(n)
SC: O(1)
Leetcode: Solve this problem
Issue: Optimized with no issue.
'''
def productExceptSelf(self, nums):
n = len(nums)
res = [1] * n
rp = 1
for i in range(1, n):
rp *= nums[i - 1]
res[i] = rp
rp = 1
for i in range(n - 2, -1, -1):
rp *= nums[i + 1]
res[i] *= rp

return res



s = Solution()
nums = [1,2,3,4]
print(s.productExceptSelf(nums))
nums = [-1,1,0,-3,3]
print(s.productExceptSelf(nums))
44 changes: 44 additions & 0 deletions spiral-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution:
'''
TC: O(m *n)
SC: O(1)
Leetcode: Completed the problem with no issue.
intuation: Use 4 pointer top, left, right, and bottom. use them to traverse the matrix to get the spiral.
'''
def spiralOrder(self, matrix):
m = len(matrix)
n = len(matrix[0])
left = 0
right = n - 1
top = 0
bottom = m - 1
res = []
while left <= right and top <= bottom:
# top
for i in range(left, right + 1):
res.append(matrix[top][i])
top += 1

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

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


s = Solution()
matrix = [[1,2,3],[4,5,6],[7,8,9]]
print(s.spiralOrder(matrix))
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
print(s.spiralOrder(matrix))