Skip to content
Open

Array-1 #1946

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
52 changes: 52 additions & 0 deletions matrix_diagonal_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
time o(n) where n = number of elements in matrix
space o(1)
create a result array and keep adding elements based on rules ot the result array.
Rules are based on index values
if element in top row, move to right and change direction
if element in right boundary column, move down and change direction
if element in bottom row, move right and change direction
if element in left boundary row, move down and change direction
"""

from typing import List


class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
result = []
m = len(mat)
n = len(mat[0])
total = m * n
x = y = 0
up = True
while len(result) < total:
print(x, y)
result.append(mat[x][y])
if up:
if x == 0 and y == len(mat[0]) - 1:
x += 1
up = False
elif x == 0 and y < len(mat[0]) - 1:
y += 1
up = False
elif x != 0 and y == len(mat[0]) - 1:
x += 1
up = False
else:
x -= 1
y += 1
else:
if x == len(mat) - 1 and y == 0:
y += 1
up = True
elif x == len(mat) - 1 and y > 0:
y += 1
up = True
elif x != 0 and y == 0:
x += 1
up = True
else:
x += 1
y -= 1
return result
26 changes: 26 additions & 0 deletions prod_except_self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
time - o(n)
space o(n)

we iterate forward and take the running product of all elements and store it in an array
we do the same backwards and multiply it with the existing forward running product values
"""

from typing import List


class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result = [0] * len(nums)
product = 1
for i in range(len(nums)):
result[i] = product
product *= nums[i]
product = 1
for i in range(len(nums) - 1, -1, -1):
if i == 0:
result[i] = product
else:
result[i] = product * result[i]
product *= nums[i]
return result
35 changes: 35 additions & 0 deletions spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
time - o(n)
space o(1)
We maintain four boundaries left, right, top and bottom and move from left to right, top to bottom and right to left and bottom to top
as we keep moving we increment/decrement the boundaries.
"""

from typing import List


class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
result = []
left, right, top, bottom = 0, len(matrix[0]) - 1, 0, len(matrix) - 1
while left <= right and top <= bottom:
for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1

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

if right >= left and top <= bottom:
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1

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

return result