From 5f5e2f1c447b292d9da7bb6ad9112e936dea6a0e Mon Sep 17 00:00:00 2001 From: Purva Date: Wed, 26 Nov 2025 02:08:47 -0600 Subject: [PATCH] done --- problem1.py | 31 +++++++++++++++++++++++++++++++ problem2.py | 40 ++++++++++++++++++++++++++++++++++++++++ problem3.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py create mode 100644 problem3.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..46f1f55b --- /dev/null +++ b/problem1.py @@ -0,0 +1,31 @@ +""" +We use a two pass approach to overcome the exponential brute force approach with i and j pointers, we calculate the product of elements to the left of number and to +the right of the product, this way we can multiply the running product and get our answer +TC is o(n) for traversing the whole array and SC is o(1) (resulant array doesnt count towards space) +""" + + +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + #do a two pass approach, one left pass and one right pass + + n = len(nums) + res = [0] * n + rp = 1 + res[0] = 1 + #left pass + for i in range(1,n): + rp = rp * nums[i-1] + res[i] = rp + + rp = 1 #reset for right pass + for i in range(n-2, -1, -1): + rp = rp * nums[i + 1] + res[i] = res[i] * rp + + return res + diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..11eac010 --- /dev/null +++ b/problem2.py @@ -0,0 +1,40 @@ +""" +The idea is to see which conditions we hit at each pass and update the rows and cols accordingly +the TC is o(m*n) and SC is O(1) since we build on result array and not on aux array +""" +class Solution(object): + def findDiagonalOrder(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + m = len(mat) + n = len(mat[0]) #column + res = [0] * (m*n) + r, c = 0,0 + flag = True + + for i in range(m*n): + res[i] = mat[r][c] + if flag: #up right + if c == n - 1: #last element in the top right, increment row + r += 1 + flag = False #go down + elif r == 0: #its in the first row, increment row then flag hanges + flag = False + c += 1 #shift the column + else: #else, update the conditions to move in a up-right direction (column-increase, row decrease) + c += 1 + r -= 1 + else: + if r == m - 1: #if it is the last row, increment column and switch flag + flag = True + c += 1 + elif c == 0: #if its the the first column, increment the row + flag = True + r += 1 + else: + r += 1 + c -= 1 + return res + diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..e493f422 --- /dev/null +++ b/problem3.py @@ -0,0 +1,41 @@ +""" +We use the boundaries method and follow the path that we would usually do while traversing on a notebook, and then update the pointers +TC is o(m*n) and SC is o(1) since its directly stored in resultant space +""" + + +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + m = len(matrix) + n = len(matrix[0]) + top = 0 + bottom = m - 1 + right = n - 1 + left = 0 + res = [] + while top <= bottom and left <= right: + for i in range(left, right + 1): #right + 1 so its included + res.append(matrix[top][i]) + top += 1 + + for i in range(top, bottom + 1): + res.append(matrix[i][right]) + right -= 1 + + if top <= bottom: #we have to update the pointers, so that nothing is left nor repeated + for i in range(right, left - 1, -1): #you dont want to repeat left row again, so left - 1 + res.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + for i in range(bottom, top - 1, -1): + res.append(matrix[i][left]) + left += 1 + return res + + + \ No newline at end of file