Skip to content
Open
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
51 changes: 51 additions & 0 deletions 238. Product of Array Except Self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Time: O(n^2)
# Space: O(1)
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result = [1] * len(nums)
for i in range(len(result)):
for j in range(len(nums)):
if j != i:
result[i] *= nums[j]
return result

# Time: O(3n) = O(n)
# Space: O(2n) for 2 seperate arrays
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
left_running = [1] * len(nums)
right_running = [1] * len(nums)
result =[0] * len(nums)

for i in range(1,len(nums)):
left_running[i] = nums[i-1] * left_running[i - 1]
for j in range(len(nums) - 2, -1, -1):
right_running[j] = nums[j+1] * right_running[j + 1]
for k in range(len(nums)):
result[k] = left_running[k] * right_running[k]

return result


# Time Complexity : O(n)
# Space Complexity : O(1) leaving the output array
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english
# - Form the result array with everything as 1
# - Do the left pass with running product
# - Then do the right pass muliplying with the current left pass elements of the array

# Your code here along with comments explaining your approach

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result =[1] * len(nums)

for i in range(1,len(nums)):
result[i] = nums[i-1] * result[i - 1]
right_running = 1
for j in range(len(nums) - 1, -1, -1):
result[j] *= right_running
right_running *= nums[j]

return result
50 changes: 50 additions & 0 deletions 498. Diagonal Traverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Time Complexity : O (m*n)
# Space Complexity : O(1) except the output array
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english
# strating from [0,0] check from which side we want to move
# depending on which side we are moving check if we are going out of bound
# if we are moving out of bound change i and j accordingly if not put a normal pass
# write j == (n - 1) before of elif i == 0 to handle the edge case when i is 0 at j == len(n-1) and then we increment and then go out of bound

# Your code here along with comments explaining your approach

class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m,n = len(mat), len(mat[0])
i = 0
j = 0
direction = True
result = []
while len(result) < m * n:
result.append(mat[i][j])
# when we are going upwards i.e direction True
if direction:
# writin this first to handle the edge case when i and j both are zero
if j == (n - 1):
direction = False
i += 1
# going out from top
elif i == 0:
direction = False
j += 1
# for normal pass
else:
i -= 1
j += 1
# when we are going downwards
else:
# going out from botom
if i == (m - 1):
direction = True
j += 1
# going out from left
elif j == 0:
direction = True
i += 1
# for normal pass
else:
i += 1
j -= 1

return result
40 changes: 40 additions & 0 deletions 54. Spiral Marix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Time: O(m*n)
# space:O(1)
# Que => Return an array by iterating through the matrix spirally
# have 4 variables to control the edges then loop while left doesn't cross right and top doesn't cross bottom
# then as we are updating the base variables keep in mind to check the conditions before performing anything

class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
n = len(matrix[0])
top = 0
right = n - 1
bottom = m - 1
left = 0

result = []
while left <= right and top <= bottom:
# Top row
for i in range(left,right+1):
result.append(matrix[top][i])
top += 1
# We are changing the base case that is why we check if it is changed again because we are changing top in previous loop
if top <= bottom:
# Right most row
for i in range(top,bottom+1):
result.append(matrix[i][right])
right -= 1

if left <= right and top <= bottom:
# Bottom row
for i in range(right,left-1,-1):
result.append(matrix[bottom][i])
bottom -= 1
if left <= right and top <= bottom:
# Left row
for i in range(bottom,top-1,-1):
result.append(matrix[i][left])
left += 1
return result