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
29 changes: 29 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Time Complexity : O(2n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english:
"""1. Optimizing brute force approach of O(n^2) buy calculating running product from left to right and right to left
2. In this solution the time complexity O(n) can be achived by using extra array of space O(n)
3. we use one for loop to find the running product from left to right and store in the res[] array then multiply this array with running product from right to left"""

# Your code here along with comments explaining your approach
class Solution(object):
def productExceptSelf(self, nums):
#initializing a result array with 1's
res=[1]*len(nums)
#variable to count running product at each iteration
#rp will store the running products of integers in the array so far
rp=1
#first for loop to calculate a running product of the given array from left to right
for i in range(1,len(nums)):
rp=rp*nums[i-1]
res[i]=rp
print(res)
#now resetting the running product variable for right running product
rp=1
#iterating from right to left to calculate running product from teh right
for j in range(len(nums)-2,-1,-1):
rp=rp*nums[j+1]
#product of current running product with the number in resultant i.e left running product
res[j]=rp*res[j]
return res
45 changes: 45 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Time Complexity : O(m * n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english:
# 1. We can use two pointers i and j to traverse the diagonals of the matrix one for row and one for column.
# 2. We can use a boolean variable to keep track of the direction of traversal (up or down).
# 3. We can append the elements to the result list based on the current direction and adjust the pointers to go in diagonal directions and until we have traversed all elements in the matrix.
# Your code here along with comments explaining your approach
class Solution(object):
def findDiagonalOrder(self, mat):
if not mat or not mat[0]:
return []

m, n = len(mat), len(mat[0])
res = []

i = j = 0
up = True
# we will traverse the matrix until we have traversed all the elements in the matrix
for k in range(m * n):
res.append(mat[i][j])
# setting directions for pointers based on which direction to traverse first we start with up direction
if up:
if j == n - 1:
i += 1
up = False
elif i == 0:
j += 1
up = False
else:
i -= 1
j += 1
# if we are traversing in down direction then we will check for out of bound condition for row and column
else:
if i == m - 1:
j += 1
up = True
elif j == 0:
i += 1
up = True
else:
i += 1
j -= 1

return res
43 changes: 43 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Time Complexity : O(m * n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Three line explanation of solution in plain english:
# We will create four pointers up, down, left and right to keep track of the boundaries of the matrix.
# We will traverse the matrix in a spiral manner by first traversing from left to right, then from top to bottom, then from right to left and finally from bottom to top.
# We will keep updating the pointers after each traversal to shrink the boundaries of the matrix and continue the traversal until we have traversed all the elements in the matrix.


# Your code here along with comments explaining your approach
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
m,n=len(matrix),len(matrix[0])
up,down=0,m-1
left,right=0,n-1
res=[]
#shrinking the spiral and making sure that we stay within the boundary without overlapping the cells
while up<=down and left<=right:
#collecting the matrix values from left to right
# j because we are iterating through columns
for j in range(left, right+1):
res.append(matrix[up][j])
#since we have finished the one row updating up to indicate we are shrinking the row from up
up+=1
for i in range(up, down+1):
res.append(matrix[i][right])
#since we have finished the one column updating right to indicate we are shrinking the column from right
right-=1
# if up is less than or equal to down then only we will traverse from right to left because if up is greater than down then it means we have already traversed all the rows and we should not traverse again
if up <= down:
for j in range(right, left - 1, -1):
res.append(matrix[down][j])
down -= 1
#now if left is less than or equal to right then only we will traverse from bottom to top because if left is greater than right then it means we have already traversed all the columns and we should not traverse again
if left <= right:
for i in range(down, up - 1, -1):
res.append(matrix[i][left])
left += 1
return res