diff --git a/matrix_diagonal_traversal.py b/matrix_diagonal_traversal.py new file mode 100644 index 00000000..eb2858b6 --- /dev/null +++ b/matrix_diagonal_traversal.py @@ -0,0 +1,52 @@ +""" +time o(n) where n = number of elements in matrix +space o(1) +create a result array and keep adding elements based on rules ot the result array. +Rules are based on index values +if element in top row, move to right and change direction +if element in right boundary column, move down and change direction +if element in bottom row, move right and change direction +if element in left boundary row, move down and change direction +""" + +from typing import List + + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + result = [] + m = len(mat) + n = len(mat[0]) + total = m * n + x = y = 0 + up = True + while len(result) < total: + print(x, y) + result.append(mat[x][y]) + if up: + if x == 0 and y == len(mat[0]) - 1: + x += 1 + up = False + elif x == 0 and y < len(mat[0]) - 1: + y += 1 + up = False + elif x != 0 and y == len(mat[0]) - 1: + x += 1 + up = False + else: + x -= 1 + y += 1 + else: + if x == len(mat) - 1 and y == 0: + y += 1 + up = True + elif x == len(mat) - 1 and y > 0: + y += 1 + up = True + elif x != 0 and y == 0: + x += 1 + up = True + else: + x += 1 + y -= 1 + return result diff --git a/prod_except_self.py b/prod_except_self.py new file mode 100644 index 00000000..1e9f8663 --- /dev/null +++ b/prod_except_self.py @@ -0,0 +1,26 @@ +""" +time - o(n) +space o(n) + +we iterate forward and take the running product of all elements and store it in an array +we do the same backwards and multiply it with the existing forward running product values +""" + +from typing import List + + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + result = [0] * len(nums) + product = 1 + for i in range(len(nums)): + result[i] = product + product *= nums[i] + product = 1 + for i in range(len(nums) - 1, -1, -1): + if i == 0: + result[i] = product + else: + result[i] = product * result[i] + product *= nums[i] + return result diff --git a/spiral_matrix.py b/spiral_matrix.py new file mode 100644 index 00000000..a6c52a61 --- /dev/null +++ b/spiral_matrix.py @@ -0,0 +1,35 @@ +""" +time - o(n) +space o(1) +We maintain four boundaries left, right, top and bottom and move from left to right, top to bottom and right to left and bottom to top +as we keep moving we increment/decrement the boundaries. +""" + +from typing import List + + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + result = [] + left, right, top, bottom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1 + while left <= right and top <= bottom: + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + if top <= bottom and left <= right: + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + if right >= left and top <= bottom: + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + if top <= bottom and left <= right: + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result