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
64 changes: 64 additions & 0 deletions diagonal_traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# https://leetcode.com/problems/diagonal-traverse/description/

# In this program to move alternately up-right and down-left defined the direction flag. Starting at the top left it moves diagonally untill the last column and last row. And it stores the element in the result array.
# Time Complexity- O(m * n) Space Complexity- O(1)

class Solution:
def findDiagonalOrder(self, mat: list[list[int]]) -> list[int]:
# find the number of rows
m = len(mat)
# find the length of columns
n = len(mat[0])
# defining the array to store the result
result = [0] * (m * n)
# starting from the top right corner
r = c = 0
# flag to show the direction to move
dir = True
# iterate over the matrix
for i in range(m * n):
# store the current matrix value into the result
result[i] = mat[r][c]

# if direction is true move up-right
if dir:
# checking whether we are at the last column
if c == n-1:
# move to next row
r += 1
# change the flag(as we are going down)
dir = False
# checking whether we are at the top row(can not go up)
elif r == 0:
# move to next column
c += 1
# change the flag
dir = False
# move diagonally
else:
r -= 1
c += 1
else:
# checking whether we are at the last row(bottom)
if r == m -1:
# move to the next column
c += 1
# change the flag(as we are going up)
dir = True
# checking whether we are the edge column(can not go left)
elif c == 0:
# move to the down
r += 1
# change the flag
dir = True
# move diagonally
else:
r += 1
c -= 1
return result


solution = Solution()
print(solution.findDiagonalOrder([[1,2,3],[4,5,6],[7,8,9]
]))
print(solution.findDiagonalOrder([[1,2],[3,4]]))
33 changes: 33 additions & 0 deletions product_of_array_except_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# https://leetcode.com/problems/product-of-array-except-self/description/

# Used the runnig product approach. In this with the loop find out the product of all elements to the left of each index stored in result array at the index position. Second loop is used to find the product of all element to the right of each index and multiply with the result array.
# Time Complexity: O(n) Space Complexity: O(1)


class Solution:
def productExceptSelf(self, nums: list[int]) -> list[int]:
n = len(nums)
result = [0] * n
# initializing the running product with 1
running_product = 1
# nothing at the left for the 1st element
result[0] = 1
# left side running product, as we set 0th element at 1 so starting from 1st position
for i in range(1,n):
# we want left side product so multiplying till the i - 1
running_product = running_product * nums[i - 1]
# at ith position we are storing the product of the left side numbers
result[i] = running_product

running_product = 1
# right side running product starting from second last element
for i in range(n-2, -1, -1):
# we want right side product so multiplying from i + 1
running_product = running_product * nums[i + 1]
# at ith position we are storing the product of the numbers
result[i] = result[i] * running_product

return result

solution = Solution()
print(solution.productExceptSelf([1,2,3,4]))
43 changes: 43 additions & 0 deletions spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# https://leetcode.com/problems/spiral-matrix/

# To implement the spiral matrix used four boundries top, bottom, left and right. Using these boundries we traverse right, down, left and up. Used while loop to check the boudries are crossing each other, if they do stop the loop.
# Time Complexity- O(m * n) Space Complexity- O(1)

class Solution:
def spiralOrder(self, matrix: list[list[int]]) -> list[int]:
m = len(matrix)
n = len(matrix[0])

top, bottom , left, right = 0, m - 1, 0, n - 1
result = []
# condition to make sure top and bottm, left and right should not cross each other
while top <= bottom and left <= right:
# append the top row from left to right
for i in range(left, right + 1):
result.append(matrix[top][i])
# move top row to below row
top += 1
# append the rightmost column from top to bottom
for i in range(top, bottom + 1):
result.append(matrix[i][right])
# move to inner column(right to left)
right -= 1
# append the bottom row from right to left
if top <= bottom:
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
# move the bottom row up
bottom -= 1
# append the leftmost column from bottom to top
if left <= right:
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
# move the left boundry right
left += 1

return result


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