diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..b6509033 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,34 @@ +class Solution(object): +# tc : O(2n) 2 for loops -> O(n) +# sc : O(1) no extra space used just variables which is O(1), the product array is the output array of space n and given +# The output array does not count as extra space for space complexity analysis +# // Did this code successfully run on Leetcode : Yes + + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + # since we cannot use division operation + # at every point I will need left of the elements product and right of the elements product + # first traversing left of each index and saving the left product at that index in an product_array + # and then have one more for loop to multiple the left product with the right product at that index + # will return the product array which is the product of all elemenst except self + + + product_array = [1]*len(nums) + + right_product = 1 + left_product = 1 + + for i in range(1,len(nums)): + left_product = left_product*nums[i-1] + product_array[i] = left_product + + for i in range(len(nums)-2, -1, -1): + right_product = right_product*nums[i+1] + product_array[i] = product_array[i]*right_product + + return product_array + + diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..f872f594 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,70 @@ +class Solution(object): +# // Time Complexity : O(n*m) +# // Space Complexity : O(1) +# // Did this code successfully run on Leetcode : Yes + def findDiagonalOrder(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + # diagonal traverse : down to top and then top to down and keeps gng + # so in the matrix there are two directions upward and downward + # will write conditions to traverse from top and botton directions and cover all the elements + + + # top direction + # when at a position r, c keep iterating till top by moving r--, c++ + # the boundary conditions are r == 0 or c == n-1 where n is the number of columns - + # change the direction to bottom + # when reached r ==0, c++ move to the right + # when reached c = n-1 , r ++ move down + + # bottom direction + # when at a position r,c , keep iterating r++, c-- + # the boundary condtions are c == 0 reached till end, r == m-1 where m is the number of rows + # change the direction to top + # when reached c == 0, r ++ move to down + # when reached r = m-1 , c ++ move right + + dir = True + m = len(mat) + n = len(mat[0]) + r = c = 0 + + traverse = [] + + for i in range(m*n): + traverse.append(mat[r][c]) + + if dir: + if c == n-1: + r +=1 + dir = False # chaging the dir + elif r == 0: + c +=1 + dir = False # changing the dir + else : + r-= 1 + c +=1 + else: + if r == m-1: + c +=1 + dir = True + elif c == 0: + r +=1 + dir = True + else: + r+=1 + c -= 1 + + + return traverse + + + + + + + + + \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..b0d89f08 --- /dev/null +++ b/Problem3.py @@ -0,0 +1,37 @@ +class Solution: + # // Time Complexity : O(n*m) # traverse the whole matrix +# // Space Complexity : O(1) +# // Did this code successfully run on Leetcode : Yes + def spiralOrder(self, matrix): + # sprial matrix is move to the right and once reached till end we move till the bottom and end till the left of the matrix + # and till 1 less than the top and keep gng and once top or bottom are crossed or left and right are crossed we reached the end point + # of the spiral matrix + + m = len(matrix) + n = len(matrix[0]) + + top, bottom, left, right = 0, m - 1, 0, n - 1 + + result = [] + + while top <= bottom and left <= right: # once goes out of the this condition we have traversed all the elements in the matrix + + for i in range(left, right + 1): + result.append(matrix[top][i]) # moved till the end of the top row + top += 1 # the new top will be 1+ the previous top, as we already parsed the top elemenst the new top will be 1+ + + for i in range(top, bottom + 1): # next moved till the bottom of thr right col + result.append(matrix[i][right]) + right -= 1 # the new right will be -1 as the most right coloum is traversed the new right is -1 of the previous right + + if top <= bottom: # we have increased the top after the 1st for loop so have to check by increasing is it less than or equal to bottom , before traversing + for i in range(right, left - 1, -1): # move till the left of the bottom row + result.append(matrix[bottom][i]) + bottom -= 1 # covered the last bottom the new bottom will be 1 up so bottom-=1 + + if left <= right: # last direction is + for i in range(bottom, top - 1, -1): # move till the top of the left col + result.append(matrix[i][left]) + left += 1 + + return result