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
34 changes: 34 additions & 0 deletions Problem_1_ProductOfArrayExceptSelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
In this problem, I need to find the product of all the elements except the current elemnt.
I took a result matrix, initially to store product of elements which are on left of the current element, while multiplying to a running sum variable
Once the left product is calculated, then to the same array right elements product is calculated and multiplied on the same elements
'''
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
runningProduct = 1
n = len(nums)
# result array to store the product
result = [0]*n
result[0] = 1

# left pass
for i in range(1, n):
runningProduct = runningProduct * nums[i-1]
result[i] = runningProduct

# resetting runningProduct to 1
runningProduct = 1

# right pass
for i in range(n-2, -1, -1):
runningProduct = runningProduct * nums[i+1]
result[i] = result[i] * runningProduct

return result

'''
Time Complexity: O(n)
Since we are iterating on the matrix 2 time time complexity would be O(2n), since constants can be ignored, O(n)
Space Complexity: O(n)
Since we are taking a array of length n to store the product array.
'''
47 changes: 47 additions & 0 deletions Problem_2_DiagonalTraverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
In this problem, I solved it using a direction flag, which represents that we are moving towards up when its true and down when false.
By iterativly going through the matrix:
For upward direction iteration: After reaching the top most row, row becomes zero, so I incremented col and direction is changed
When we reach last col, it goes out of bounds, row is incremented and direction is changed.
For downward direction iteration: After reaching last first col, col becomes zero, so I incremented row and direction is changed
When we reach last row, it goes out of bounds, col is incremented and direction is changed.
'''
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m, n = len(mat), len(mat[0])

result = [0]*(m*n)
row, col = 0, 0
direction = True # upwards direction
for i in range(m*n):

result[i] = mat[row][col]
if direction:
# Upwards
if row == 0 and col != n-1:
direction = False
col += 1
elif col == n-1:
direction = False
row += 1
else:
row -= 1
col += 1
else:
# down
if col == 0 and row != m-1:
direction = True
row += 1
elif row == m-1:
col += 1
direction = True
else:
row += 1
col -= 1
return result
'''
Time Complexity: O(m*n)
since we are iterating on the matrix of length m*n
Space Complexity: O(1)
We did not take any extra space apart from variables
'''
43 changes: 43 additions & 0 deletions Problem_3_SpiralMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
In this problem, to print the matrix elements in spiral order, I considered 4 pointers(top, left, right, bottom). Each representing 4 sides of the matrix.
We start with printing top most row and then move to right most col, then the bottom row and left col. For every iteration top and left pointers are incremented and
right and bottom pointers are decremented, since we need to move towards inside in spiral order.
'''
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:

m = len(matrix)
n = len(matrix[0])

# taking 4 pointers
top, left = 0, 0
bottom, right = m-1, n-1

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

'''
Time Complexity: O(m*n)
Since we are iterating on a matrix of size m*n
Space Complexity: O(1)
We did not take any extra apart from variables.
'''