diff --git a/diagonal-traverse.py b/diagonal-traverse.py new file mode 100644 index 00000000..e420be40 --- /dev/null +++ b/diagonal-traverse.py @@ -0,0 +1,40 @@ +''' Time Complexity : O(m*n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Approach : If we reach top or right boundary: flip the direction to downward + if at left and bottom boundary flip the direction to upward + handle the edge case at corners +''' +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + rows=len(mat) + cols=len(mat[0]) + result=[] + flag=True + r,c = 0, 0 + for i in range(rows*cols): + result.append(mat[r][c]) + if flag: + if c == cols-1: + r += 1 + flag=False + elif r == 0: + c += 1 + flag =False + else: + r -= 1 + c += 1 + else: + if r == rows-1: + c += 1 + flag=True + elif c == 0: + r += 1 + flag =True + else: + r += 1 + c -= 1 + + return result \ No newline at end of file diff --git a/product-of-array-except-self.py b/product-of-array-except-self.py new file mode 100644 index 00000000..a58baebc --- /dev/null +++ b/product-of-array-except-self.py @@ -0,0 +1,22 @@ +''' Time Complexity : O(n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Approach : First do left pass to calculate running product. + Then second iteration from right to left and updating the new product +''' + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n =len(nums) + L = [1] * n + prev, post = 1,1 + for i in range(n): + L[i] = prev + prev = prev * nums[i] + for j in range(n-1,-1,-1): + L[j] = post * L[j] + post = post * nums[j] + + return L \ No newline at end of file diff --git a/spiral-matrix.py b/spiral-matrix.py new file mode 100644 index 00000000..ab26e6d5 --- /dev/null +++ b/spiral-matrix.py @@ -0,0 +1,73 @@ +''' Time Complexity : O(m*n) + Space Complexity : O(1) + Did this code successfully run on Leetcode : Yes + Any problem you faced while coding this : No + + Approach : First do left pass to calculate running product. + Then second iteration from right to left and updating the new product +''' + +#---------- Solution 1 : Checking the variable before every for loop iteration-------- +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + rows,cols= len(matrix),len(matrix[0]) + l, t = 0,0 + r, b = cols-1,rows-1 + result = [] + while l <= r and t <= b: + #for left to right + for i in range(l,r + 1): + result.append(matrix[t][i]) + t += 1 + + if l <= r and t <= b: + #for top to bottom + for i in range(t,b + 1): + result.append(matrix[i][r]) + r -= 1 + + if l <= r and t <= b: + #for right to left + for i in range(r,l-1, -1): + result.append(matrix[b][i]) + b -= 1 + + if l <= r and t <= b: + #for bottom to top + for i in range(b,t-1,-1): + result.append(matrix[i][l]) + l += 1 + return result + +#---------- Solution 2 : Optimized condition check before requied for loop iteration-------- +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + rows,cols= len(matrix),len(matrix[0]) + l, t = 0,0 + r, b = cols-1,rows-1 + result = [] + while l <= r and t <= b: + #for left to right + for i in range(l,r + 1): + result.append(matrix[t][i]) + t += 1 + + #for top to bottom + for i in range(t,b + 1): + result.append(matrix[i][r]) + r -= 1 + + if t <= b: + #for right to left + for i in range(r,l-1, -1): + result.append(matrix[b][i]) + b -= 1 + + if l <= r: + #for bottom to top + for i in range(b,t-1,-1): + result.append(matrix[i][l]) + l += 1 + return result + + \ No newline at end of file