diff --git a/DiagonalTraversal.py b/DiagonalTraversal.py new file mode 100644 index 00000000..412a81d9 --- /dev/null +++ b/DiagonalTraversal.py @@ -0,0 +1,36 @@ +# Time Complexity --> O(m*n) +# Space Complexity --> O(1). There is no auxillary space +# Approach --> We traverse through the matrix in such a way that at each boundary, we create rules that change the direction and the way we tranverse to next within bounds element +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m = len(mat) + n = len(mat[0]) + + i = 0 + j = 0 + re = [0]*(m*n) + upflag = 1 + for k in range(m*n): + re[k] = mat[i][j] + if upflag==1: + if j==n-1: + i = i+1 + upflag = 0 + elif i==0: + j = j+1 + upflag = 0 + else: + i = i-1 + j = j+1 + else: + if i==m-1: + j = j+1 + upflag = 1 + elif j==0: + i = i+1 + upflag = 1 + else: + i = i+1 + j = j-1 + return re + diff --git a/ProductOfArrayExceptSelf.py b/ProductOfArrayExceptSelf.py new file mode 100644 index 00000000..b532a317 --- /dev/null +++ b/ProductOfArrayExceptSelf.py @@ -0,0 +1,35 @@ +# Time Complexity --> O(n) +# Space Complexity --> O(1) There is no auxillary space +# Approach --> At each index, we find the running product to the left and right of it individually and then produce the result using their product. +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [] + # Find the running prod to the left of each element + lprod = 1 + answer.append(lprod) + for i in range(1, len(nums)): + lprod = lprod * nums[i-1] + answer.append(lprod) + + rprod = 1 + for i in range(len(nums)-2,-1,-1): + rprod = rprod * nums[i+1] + answer[i] = answer[i]*rprod + return answer + + +''' +# Time Complexity --> O(n*2) +# Space Complexity --> O(1) There is no auxillary space +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + answer = [] + for i in range(len(nums)): + prod = 1 + for j in range(len(nums)): + if i!=j: + prod = prod*nums[j] + answer.append(prod) + return answer + +''' diff --git a/SpiralMatrix.py b/SpiralMatrix.py new file mode 100644 index 00000000..0cb86106 --- /dev/null +++ b/SpiralMatrix.py @@ -0,0 +1,35 @@ +# Time Complexity --> O(m*n) +# Space Complexity --> O(1) as there is no auxillary space utilized +# Approach --> We start off with boundaries on 4 sides of the matrix and as we traverse through a row or a column, we manipulate the boundary values to avoid traversing the same elements. +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m = len(matrix) + n = len(matrix[0]) + top, bottom = 0, m-1 + left, right = 0, n-1 + + re = [] + while top<=bottom and left<=right: + # left to right + for j in range(left, right+1): + re.append(matrix[top][j]) + top = top+1 + + # top to bottom + for i in range(top, bottom+1): + re.append(matrix[i][right]) + right = right-1 + + if top<=bottom: + # right to left + for j in range(right, left-1, -1): + re.append(matrix[bottom][j]) + bottom = bottom-1 + + if left<=right: + # bottom to top + for i in range(bottom, top-1, -1): + re.append(matrix[i][left]) + left = left+1 + + return re