diff --git a/Problem_1_ProductOfArrayExceptSelf.py b/Problem_1_ProductOfArrayExceptSelf.py new file mode 100644 index 00000000..4c09ad0c --- /dev/null +++ b/Problem_1_ProductOfArrayExceptSelf.py @@ -0,0 +1,34 @@ +''' +In this problem, I need to find the product of all the elements except the current elemnt. +I took a result matrix, initially to store product of elements which are on left of the current element, while multiplying to a running sum variable +Once the left product is calculated, then to the same array right elements product is calculated and multiplied on the same elements +''' +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + runningProduct = 1 + n = len(nums) + # result array to store the product + result = [0]*n + result[0] = 1 + + # left pass + for i in range(1, n): + runningProduct = runningProduct * nums[i-1] + result[i] = runningProduct + + # resetting runningProduct to 1 + runningProduct = 1 + + # right pass + for i in range(n-2, -1, -1): + runningProduct = runningProduct * nums[i+1] + result[i] = result[i] * runningProduct + + return result + +''' +Time Complexity: O(n) +Since we are iterating on the matrix 2 time time complexity would be O(2n), since constants can be ignored, O(n) +Space Complexity: O(n) +Since we are taking a array of length n to store the product array. +''' \ No newline at end of file diff --git a/Problem_2_DiagonalTraverse.py b/Problem_2_DiagonalTraverse.py new file mode 100644 index 00000000..8dd5c7f6 --- /dev/null +++ b/Problem_2_DiagonalTraverse.py @@ -0,0 +1,47 @@ +''' +In this problem, I solved it using a direction flag, which represents that we are moving towards up when its true and down when false. +By iterativly going through the matrix: +For upward direction iteration: After reaching the top most row, row becomes zero, so I incremented col and direction is changed + When we reach last col, it goes out of bounds, row is incremented and direction is changed. +For downward direction iteration: After reaching last first col, col becomes zero, so I incremented row and direction is changed + When we reach last row, it goes out of bounds, col is incremented and direction is changed. +''' +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m, n = len(mat), len(mat[0]) + + result = [0]*(m*n) + row, col = 0, 0 + direction = True # upwards direction + for i in range(m*n): + + result[i] = mat[row][col] + if direction: + # Upwards + if row == 0 and col != n-1: + direction = False + col += 1 + elif col == n-1: + direction = False + row += 1 + else: + row -= 1 + col += 1 + else: + # down + if col == 0 and row != m-1: + direction = True + row += 1 + elif row == m-1: + col += 1 + direction = True + else: + row += 1 + col -= 1 + return result +''' +Time Complexity: O(m*n) +since we are iterating on the matrix of length m*n +Space Complexity: O(1) +We did not take any extra space apart from variables +''' \ No newline at end of file diff --git a/Problem_3_SpiralMatrix.py b/Problem_3_SpiralMatrix.py new file mode 100644 index 00000000..60996acb --- /dev/null +++ b/Problem_3_SpiralMatrix.py @@ -0,0 +1,43 @@ +''' +In this problem, to print the matrix elements in spiral order, I considered 4 pointers(top, left, right, bottom). Each representing 4 sides of the matrix. +We start with printing top most row and then move to right most col, then the bottom row and left col. For every iteration top and left pointers are incremented and +right and bottom pointers are decremented, since we need to move towards inside in spiral order. +''' +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + + m = len(matrix) + n = len(matrix[0]) + + # taking 4 pointers + top, left = 0, 0 + bottom, right = m-1, n-1 + + result = [] + while(top <= bottom and left <= right): + # top row + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + # right col + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + # bottom row + if top <= bottom: + for i in range(right, left-1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + # left col + if left <= right: + for i in range(bottom, top-1, -1): + result.append(matrix[i][left]) + left += 1 + return result + +''' +Time Complexity: O(m*n) +Since we are iterating on a matrix of size m*n +Space Complexity: O(1) +We did not take any extra apart from variables. +''' \ No newline at end of file