Skip to content
Open

done #1923

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -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

40 changes: 40 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -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

41 changes: 41 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -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