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
28 changes: 28 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Time Complexity :O(n)
Space Complexity : O(1)
Did this code successfully run on Leetcode :yes


Approach
we are doing to loops for counting all the value and store it in a seperate list
first left to right, we skip the first and put a 1 at index 0 and we calculate the running sum
one the second pas we calculate everything right to left leaving the last element plus while doing this we also multiple the number
with value of the pervious pass at the particular index
"""

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
rp = 1
n = len(nums)
print(n)
result = [0 for i in range(0,n)]
result[0] = 1
for i in range (1,n):
rp *= nums[i-1]
result[i] = rp
rp = 1
for i in range(n-2,-1,-1):
rp *= nums[i+1]
result[i] *= rp
return result
48 changes: 48 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Time Complexity :O(n*m)
Space Complexity : O(1)
Did this code successfully run on Leetcode :yes


Approach
we are maintain a direction pointer that will decide if we go up or down
while we are traversing the matrix and changing direction there
are two edge case we need to be know first is when row is 0 and col and len
and when row is len and col is zero

"""



class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
m = len(mat)
n = len(mat[0])
direction = True
result = [0 for _ in range(0,m*n)]
print(result)
i = 0
j = 0
for index in range(m*n):
result[index] = mat[i][j]
if direction:
if i == 0 and j !=n - 1:
direction = False
j += 1
elif j == n -1:
direction = False
i += 1
else:
i -= 1
j += 1
else:
if j == 0 and i != m - 1:
direction = True
i += 1
elif i == m -1:
direction = True
j += 1
else:
i += 1
j -= 1
return result
52 changes: 52 additions & 0 deletions problem3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Time Complexity :O(n*m)
Space Complexity : O(1)
Did this code successfully run on Leetcode :yes


Approach
we are suing 4 variables top bottom right and left to keep track of which element have been traversed
we traversing from the top, right, bottom, left
we keep doing this until we have traversed all the elements

"""


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

top += 1

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

right -= 1

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

bottom -= 1

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

left += 1
return result