From 54c90c233de4df12a03c96fbc69918482558f112 Mon Sep 17 00:00:00 2001 From: Anushri Date: Tue, 25 Nov 2025 11:43:22 -0500 Subject: [PATCH 1/5] brutforce --- product_of_array_except_self.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 product_of_array_except_self.py diff --git a/product_of_array_except_self.py b/product_of_array_except_self.py new file mode 100644 index 00000000..d8283d2b --- /dev/null +++ b/product_of_array_except_self.py @@ -0,0 +1,43 @@ +# https://leetcode.com/problems/product-of-array-except-self/description/ + +# brutforce - O(n2) with two loops when i != j do product +class Solution: + def productExceptSelf(self, nums: list[int]) -> list[int]: + n = len(nums) + result = [0] * n + for i in range(n): + # to set the product equals to 1 for every begining of the i + product = 1 + for j in range(n): + if i!=j: + product *= nums[j] + print(product) + result[i] = product + return result + + +solution = Solution() +print(solution.productExceptSelf([1,2,3,4])) + + +# class Solution: +# def productExceptSelf(self, nums: list[int]) -> list[int]: +# n = len(nums) +# result = [0] * n + +# running_product = 1 +# result[0] = 1 +# # left side running product +# for i in range(1,n): +# running_product = running_product * nums[i - 1] +# result[i] = running_product + +# running_product = 1 +# for i in range(n-2, -1, -1): +# running_product = running_product * nums[i + 1] +# result[i] = result[i] * running_product + +# return result + +# solution = Solution() +# print(solution.productExceptSelf([1,2,3,4])) \ No newline at end of file From c7750ec5c5d57a9280e608b4cb64bef29ca88218 Mon Sep 17 00:00:00 2001 From: Anushri Date: Tue, 25 Nov 2025 13:51:08 -0500 Subject: [PATCH 2/5] optimised solution --- product_of_array_except_self.py | 58 ++++++++++++++------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/product_of_array_except_self.py b/product_of_array_except_self.py index d8283d2b..d898720f 100644 --- a/product_of_array_except_self.py +++ b/product_of_array_except_self.py @@ -1,43 +1,33 @@ # https://leetcode.com/problems/product-of-array-except-self/description/ -# brutforce - O(n2) with two loops when i != j do product +# 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 - for i in range(n): - # to set the product equals to 1 for every begining of the i - product = 1 - for j in range(n): - if i!=j: - product *= nums[j] - print(product) - result[i] = product - return result - - -solution = Solution() -print(solution.productExceptSelf([1,2,3,4])) + # 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 - -# class Solution: -# def productExceptSelf(self, nums: list[int]) -> list[int]: -# n = len(nums) -# result = [0] * n - -# running_product = 1 -# result[0] = 1 -# # left side running product -# for i in range(1,n): -# running_product = running_product * nums[i - 1] -# result[i] = running_product - -# running_product = 1 -# for i in range(n-2, -1, -1): -# running_product = running_product * nums[i + 1] -# result[i] = result[i] * running_product + running_product = 1 + # right side running product starting from second last element because we dont need to check for the last element as we already defined 1 + 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 right side numbers + result[i] = result[i] * running_product -# return result + return result -# solution = Solution() -# print(solution.productExceptSelf([1,2,3,4])) \ No newline at end of file +solution = Solution() +print(solution.productExceptSelf([1,2,3,4])) \ No newline at end of file From f97c5737f2644cf05e2bf8f68031a389fcbc4840 Mon Sep 17 00:00:00 2001 From: Anushri Date: Tue, 25 Nov 2025 15:54:14 -0500 Subject: [PATCH 3/5] changes for optimised solution --- product_of_array_except_self.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/product_of_array_except_self.py b/product_of_array_except_self.py index d898720f..1e405bdb 100644 --- a/product_of_array_except_self.py +++ b/product_of_array_except_self.py @@ -20,11 +20,11 @@ def productExceptSelf(self, nums: list[int]) -> list[int]: result[i] = running_product running_product = 1 - # right side running product starting from second last element because we dont need to check for the last element as we already defined 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 right side numbers + # at ith position we are storing the product of the numbers result[i] = result[i] * running_product return result From b74569267e0a159365a7ff6cc50fd369ee3ace29 Mon Sep 17 00:00:00 2001 From: Anushri Date: Tue, 25 Nov 2025 18:37:46 -0500 Subject: [PATCH 4/5] implementing optimal solution --- diagonal_traverse.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 diagonal_traverse.py 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 From 2e4df1a2f2bb7f7bd6135e1aa8ff3074ed05b308 Mon Sep 17 00:00:00 2001 From: Anushri Date: Wed, 26 Nov 2025 11:55:17 -0500 Subject: [PATCH 5/5] optimised solution --- spiral_matrix.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spiral_matrix.py 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