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
18 changes: 18 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#Problem 238. PRODUCT OF ARRAY EXCEPT SELF
# TIME COMPELXITY: O(N) where N denotes number of elements
# SPACE COMPLEXITY: O(1) given that we are returning the array as an output that we are initializing

class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
ans=[1]*len(nums)
right=1
for i in range(1,len(nums)):
ans[i]=ans[i-1]*nums[i-1]
for i in range(len(nums)-1,0,-1):
right=right*nums[i]
ans[i-1]=ans[i-1]*right
return ans
41 changes: 41 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#Problem 498: DIAGONAL TRAVERS
# TIME COMPELXITY: O(M*N) where M denotes the row and N denotes the col
# SPACE COMPLEXITY: O(1) are we only use variables to track directions and positioning

class Solution(object):
def findDiagonalOrder(self, mat):
"""
:type mat: List[List[int]]
:rtype: List[int]
"""
m=len(mat)
n=len(mat[0])
result=[]
row=0
col=0
up=True

while len(result)<m*n:
result.append(mat[row][col])

if up:
if col==n-1:
row+=1
up=False
elif row==0:
col+=1
up=False
else:
row-=1
col+=1
else:
if row==m-1:
col+=1
up=True
elif col==0:
row+=1
up=True
else:
row+=1
col-=1
return result
34 changes: 34 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Problem 54. SPIRAL MATRIX
# TIME COMPELXITY: O(M*N) where M is the number of rows and N is the nuber of cols
# SPACE COMPLEXITY: O(1) since we only use pointers to trace the matrix


class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
top=0
bottom=len(matrix)-1
left=0
right=len(matrix[0])-1
result=[]

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

return result