diff --git a/DiagonalTraverse.py b/DiagonalTraverse.py new file mode 100644 index 00000000..f39379de --- /dev/null +++ b/DiagonalTraverse.py @@ -0,0 +1,36 @@ +class Solution: + def findDiagonalOrder(self, mat: list[list[int]]) -> list[int]: + + m = len(mat) + n = len(mat[0]) + result = [0 for i in range(m*n)] + + index = 0 # index at result array + r = 0 # to iterate through rows in matrix + c = 0 # to iterate through columns in matrix + flag = 1 # upward traverse + + while index < m*n: + result[index] = mat[r][c] + index += 1 + if flag == 1: + if c == n-1: + r += 1 + flag = 0 + elif r == 0: + c += 1 + flag = 0 + else: + c += 1 + r -= 1 + else: + if r == m-1: + c += 1 + flag = 1 + elif c == 0: + r += 1 + flag = 1 + else: + r += 1 + c -= 1 + return result \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.py b/ProductOfArrayExceptSelf.py new file mode 100644 index 00000000..4be96563 --- /dev/null +++ b/ProductOfArrayExceptSelf.py @@ -0,0 +1,58 @@ +# brute force approach +class Solution: + def productExceptSelf(self, nums: list[int]) -> list[int]: + if nums == None or len(nums) == 0: + return [-1] + + n = len(nums) # 4 + result = [] + + for i in range(n): # 0 + product = 1 + for j in range(n): # 1 + if i != j: + product *= nums[j] # 1 * 2 = 2 | 2 * 3 = 6 | 6 * 4 = 24 + result.append(product) + + return result + +# optimized approach 1 +class Solution: + def productExceptSelf(self, nums: list[int]) -> list[int]: + if nums == None or len(nums) == 0: + return [-1] + + n = len(nums) + + left_arr = [1] * n + for i in range(1,n): + left_arr[i] = nums[i-1] * left_arr[i-1] + + right_arr = [1] * n + for i in range(n-2,-1,-1): # usualy to iterate reverse, we consider n-1. But here we want to skip the first element like we did in the left_arr. Hence n-2 + right_arr[i] = nums[i+1] * right_arr[i+1] # we are doing +1 since we need to capture the product from right side. If we do -1 : -2-1 = -3, that goes to the left element. + + result = [] + for i in range(n): + result.append(left_arr[i]*right_arr[i]) + + return result + +# optimized approach 2 +class Solution: + def productExceptSelf(self, nums: list[int]) -> list[int]: + if nums == None or len(nums) == 0: + return [-1] + + n = len(nums) + result = [1] * n + rp = 1 # running product + + for i in range(1,n): + result[i] = result[i-1] * nums[i-1] + + for i in range(n-1,-1,-1): + result[i] = rp * result[i] + rp *= nums[i] + + return result \ No newline at end of file diff --git a/SprialMatrix.py b/SprialMatrix.py new file mode 100644 index 00000000..a8a459b0 --- /dev/null +++ b/SprialMatrix.py @@ -0,0 +1,40 @@ +class Solution: + def spiralOrder(self, matrix: list[list[int]]) -> list[int]: + + m = len(matrix) + n = len(matrix[0]) + result = [] + + top = 0 + bottom = m-1 + left = 0 + right = n-1 + + while top <= bottom and left <= right: + # result[index] = matrix[r][c] + + # top traverse + if top <= bottom and left <= right: + for i in range(left, right+1): + result.append(matrix[top][i]) + top += 1 + + # right traverse + if top <= bottom and left <= right: + for i in range(top, bottom+1): + result.append(matrix[i][right]) + right -= 1 + + # bottom traverse + if top <= bottom and left <= right: + for i in range(right, left-1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + # left traverse + if top <= bottom and left <= right: + for i in range(bottom, top-1, -1): + result.append(matrix[i][left]) + left += 1 + + return result \ No newline at end of file