diff --git a/W3_diagonal_traverse.py b/W3_diagonal_traverse.py new file mode 100644 index 00000000..4d3d2396 --- /dev/null +++ b/W3_diagonal_traverse.py @@ -0,0 +1,46 @@ +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + + if mat is None: + return [] + + + m = len(mat) + n = len(mat[0]) + + r = 0 + c = 0 + + dir = True + + result = [0] * (m * n) + + for i in range(m*n): + result[i] = mat[r][c] + + if dir: + if c == n-1: + r+= 1 + dir = False + elif r == 0: + c += 1 + dir = False + else: + r -= 1 + c += 1 + + else: + if r == m-1: + c+= 1 + dir = True + elif c == 0: + r += 1 + dir = True + else: + c -= 1 + r += 1 + + return result + +# Time: O(m * n) +# Space O(1) \ No newline at end of file diff --git a/W3_product_except_self.py b/W3_product_except_self.py new file mode 100644 index 00000000..dbad19b6 --- /dev/null +++ b/W3_product_except_self.py @@ -0,0 +1,28 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + + result = [0] * len(nums) + + if nums is None: + return result + + # Running product + rp = 1 + result[0] = 1 + + # left/forward pass + for i in range(1, len(nums)): + rp = rp * nums[i-1] + result[i] = rp + + rp = 1 + + # Right pass / backward pass + for i in range(len(nums) - 2, -1, -1): + rp = rp * nums[i+1] + result[i] = result[i] * rp + + return result + +# Time complexity: O(2n) = O(N) +# Space complexity: O(1) \ No newline at end of file