diff --git a/diagonal_traverse.py b/diagonal_traverse.py new file mode 100644 index 00000000..fe558330 --- /dev/null +++ b/diagonal_traverse.py @@ -0,0 +1,64 @@ +# https://leetcode.com/problems/diagonal-traverse/description/ + +# In this program to move alternately up-right and down-left defined the direction flag. Starting at the top left it moves diagonally untill the last column and last row. And it stores the element in the result array. +# Time Complexity- O(m * n) Space Complexity- O(1) + +class Solution: + def findDiagonalOrder(self, mat: list[list[int]]) -> list[int]: + # find the number of rows + m = len(mat) + # find the length of columns + n = len(mat[0]) + # defining the array to store the result + result = [0] * (m * n) + # starting from the top right corner + r = c = 0 + # flag to show the direction to move + dir = True + # iterate over the matrix + for i in range(m * n): + # store the current matrix value into the result + result[i] = mat[r][c] + + # if direction is true move up-right + if dir: + # checking whether we are at the last column + if c == n-1: + # move to next row + r += 1 + # change the flag(as we are going down) + dir = False + # checking whether we are at the top row(can not go up) + elif r == 0: + # move to next column + c += 1 + # change the flag + dir = False + # move diagonally + else: + r -= 1 + c += 1 + else: + # checking whether we are at the last row(bottom) + if r == m -1: + # move to the next column + c += 1 + # change the flag(as we are going up) + dir = True + # checking whether we are the edge column(can not go left) + elif c == 0: + # move to the down + r += 1 + # change the flag + dir = True + # move diagonally + else: + r += 1 + c -= 1 + return result + + +solution = Solution() +print(solution.findDiagonalOrder([[1,2,3],[4,5,6],[7,8,9] +])) +print(solution.findDiagonalOrder([[1,2],[3,4]])) \ 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..1e405bdb --- /dev/null +++ b/product_of_array_except_self.py @@ -0,0 +1,33 @@ +# https://leetcode.com/problems/product-of-array-except-self/description/ + +# Used the runnig product approach. In this with the loop find out the product of all elements to the left of each index stored in result array at the index position. Second loop is used to find the product of all element to the right of each index and multiply with the result array. +# Time Complexity: O(n) Space Complexity: O(1) + + +class Solution: + def productExceptSelf(self, nums: list[int]) -> list[int]: + n = len(nums) + result = [0] * n + # initializing the running product with 1 + running_product = 1 + # nothing at the left for the 1st element + result[0] = 1 + # left side running product, as we set 0th element at 1 so starting from 1st position + for i in range(1,n): + # we want left side product so multiplying till the i - 1 + running_product = running_product * nums[i - 1] + # at ith position we are storing the product of the left side numbers + result[i] = running_product + + running_product = 1 + # right side running product starting from second last element + for i in range(n-2, -1, -1): + # we want right side product so multiplying from i + 1 + running_product = running_product * nums[i + 1] + # at ith position we are storing the product of the numbers + result[i] = result[i] * running_product + + return result + +solution = Solution() +print(solution.productExceptSelf([1,2,3,4])) \ No newline at end of file diff --git a/spiral_matrix.py b/spiral_matrix.py new file mode 100644 index 00000000..fac37246 --- /dev/null +++ b/spiral_matrix.py @@ -0,0 +1,43 @@ +# https://leetcode.com/problems/spiral-matrix/ + +# To implement the spiral matrix used four boundries top, bottom, left and right. Using these boundries we traverse right, down, left and up. Used while loop to check the boudries are crossing each other, if they do stop the loop. +# Time Complexity- O(m * n) Space Complexity- O(1) + +class Solution: + def spiralOrder(self, matrix: list[list[int]]) -> list[int]: + m = len(matrix) + n = len(matrix[0]) + + top, bottom , left, right = 0, m - 1, 0, n - 1 + result = [] + # condition to make sure top and bottm, left and right should not cross each other + while top <= bottom and left <= right: + # append the top row from left to right + for i in range(left, right + 1): + result.append(matrix[top][i]) + # move top row to below row + top += 1 + # append the rightmost column from top to bottom + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + # move to inner column(right to left) + right -= 1 + # append the bottom row from right to left + if top <= bottom: + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + # move the bottom row up + bottom -= 1 + # append the leftmost column from bottom to top + if left <= right: + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + # move the left boundry right + left += 1 + + return result + + +solution = Solution() +print(solution.spiralOrder([[1,2,3],[4,5,6],[7,8,9]])) +print(solution.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]])) \ No newline at end of file