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
54 changes: 54 additions & 0 deletions diagnol-traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Find the number of elements in mat and assign an arr too that length. Keep the flag true
# at first. Loop throught the elelmts in mat.
# If flag is true:
# If row is 0 and col is not the last col, increment the col and make flag=false,
# if col is last col, increment the row anf turn flag to false else decrement row and increment col
# If flag is false:
# If col is 0 and row is not the last row, increment the row and make flag=true,
# if row is last row, increment the col anf turn flag to true else increment row and decrement col




class Solution(object):
def findDiagonalOrder(self, mat):
"""
:type mat: List[List[int]]
:rtype: List[int]
"""
row = len(mat)
col = len(mat[0])
n = (row)*(col)
r=0
c=0
arr=[None for i in range(n)]
flag = True
for i in range(n):
print(r,c)
arr[i]=mat[r][c]
if flag:
if r==0 and c!=col-1:
c+=1
flag = False
elif c==col-1:
r+=1
flag=False
else:
r-=1
c+=1
else:
if c==0 and r!=row-1:
r+=1
flag=True
elif r==row-1:
c+=1
flag=True
else:
r+=1
c-=1
return arr





33 changes: 33 additions & 0 deletions product-except-self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# take s=1, left_prod array with length of nums. Loop through nums and multiply each
# element with s and update s to the new value. Store each product value in left_prod array.
# Assign s = 1 and loop through left_prod from reverse. update left_prod[i] with
# left_prod[i]*s ans update s to s*nums[i]

# Time complexity: O(n)
# Space complexity O(n)

class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
s=1
left_prod=[]
l=len(nums)
i=0
while i<l:
left_prod.append(s)
s=s*nums[i]
i+=1
s=1
i=l-1
print("left prod", left_prod)
while i>=0:
left_prod[i]=left_prod[i]*s
s=s*nums[i]
i-=1
return left_prod



44 changes: 44 additions & 0 deletions spiral-matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

# Find the no. of rows and col. Assign top=0, left=0, down = r-1 and right = c-1.
# find the total number of elemtns in the matrix as n. Assign an empty array arr. while left<=rigth and top<=down
# print the elemtns from left to right and then increment top
# print elemts from top to down and decrement right
# print elemts from right to left and decrement down
# print elemetns from down to top and increment left

# Time complexity: O(row*col)
# Space complexity: O(1)


class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
r=len(matrix)
c=len(matrix[0])
top = 0
left = 0
down = r-1
right = c-1
n = r*c
arr=[]
while left<=right and top<=down:

for i in range(left,right+1):
arr.append(matrix[top][i])
top+=1

for i in range(top,down+1):
arr.append(matrix[i][right])
right-=1
if top<=down:
for i in range(right,left-1,-1):
arr.append(matrix[down][i])
down-=1
if left<=right:
for i in range(down,top-1,-1):
arr.append(matrix[i][left])
left+=1
return arr