diff --git a/Probelm1.py b/Probelm1.py new file mode 100644 index 00000000..88d484f9 --- /dev/null +++ b/Probelm1.py @@ -0,0 +1,41 @@ +#Product of Array Except Self + +#time complexity -> O2n -> On +# Space complexity -> O1 since we are using only result array and not 2nd array + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + # generate an array from right to left saving the product till that position + # For exampe for [1,2,3,4] will be [24, 12, 4, 1] in the result array + # then in the 2nd iteration from the start position keep the product in the single variable, and for each index the result product will be + # product of the variable and the value coming from the result array at that index, the update the index value in the same reult array + length = len(nums) + resultArr = [1]*length + product = 1 + for i in range(length-1, -1, -1): + resultArr[i] = product + product *= nums[i] + product = 1 + for i in range(0,length): + resultArr[i] = product * resultArr[i] + product = product * nums[i] + return resultArr + + +# 1,2,3,4 + +# 24, 12, 4, 1 +PR = 1 + +currentPos * PR = 24, +Pr = 1*1 + +currentPos * PR = 12* 1 +pr = 1*2 = 2 + +currentPos * PR = 4 * 2 = 8 +pr = currentPos * pr = 3 * 2 = 6 + +currentPos * PR = 1 * 6 = 6 + +# 24, 12, 8, 6 \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..0ca0883c --- /dev/null +++ b/Problem2.py @@ -0,0 +1,55 @@ +#Diagonal Traverse + +# TimeComplexity -> Omxn +# Space complexity -> O1 +# Logic - > maintain the direction of the movement, when moving up, increase column by 1 and decrease row by 1, when moving down decrease column byy 1 +# and increase row by 1. While handling the edge cases at corners and borders + +# from typing import List + + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + row = 0 + column = 0 + rows = len(mat) + columns = len(mat[0]) + result = [] + resultLength = rows*columns + direction = 1 # 1 is for up and -1 is for down + + #need to loop untill the result is of direct length + while len(result) < resultLength: + if direction == 1: + while (row >= 0 and column < columns ): + result.append(mat[row][column]) + row-=1 + column+=1 + direction = -1 + if row == -1 and column == columns: + row += 2 + column-=1 + elif row == -1: + row += 1 + elif column == columns: + row += 2 + column -= 1 + + if direction == -1: + while (column >=0 and row < rows): + result.append(mat[row][column]) + row+=1 + column-=1 + direction = 1 + if column == -1 and row == rows: + column+=2 + row-=1 + elif column == -1: + column+=1 + elif row == rows: + column+=2 + row-=1 + return result + +sol = Solution() +print(sol.findDiagonalOrder([[1,2,3],[4,5,6]])) \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..a23a3ea3 --- /dev/null +++ b/Problem3.py @@ -0,0 +1,60 @@ +#Spiral Matrix + +# Time complexity -> Omxn +# Space complexity -> O1 +# Logic - > maintain the direction of the movement, when moving right, increase column by 1,moving down increse row by 1, moving left decrease column byy 1, +# moving up decrese row by 1. While handling the edge cases at borders and visited rows and columns + +from typing import List +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + rows = len(matrix) + columns = len(matrix[0]) + # maintain endpoints in each direction indicating time to change directs + columnRight = columns - 1 + columnleft = 0 + rowTop = 0 + rowBottom = rows - 1 + + resultLength = rows*columns + result = [] + + if resultLength == 1: + return [matrix[0][0]] + + #need to loop untill the result is of direct length + while len(result) < resultLength: + print(result) + # move right + for col in range(columnleft,columnRight+1): + result.append(matrix[rowTop][col]) + #rowTop needs to be incremented by 1 + rowTop+=1 + + if len(result) == resultLength: + return result + #Now move down + for row in range(rowTop,rowBottom+1): + result.append(matrix[row][columnRight]) + #columnRight needs to be decreemnted by 1 + columnRight-=1 + + if len(result) == resultLength: + return result + #Now move left + for col in range(columnRight,columnleft-1, -1): + result.append(matrix[rowBottom][col]) + #rowBottom needs to be decremented by 1 + rowBottom-=1 + + if len(result) == resultLength: + return result + #Now move up + for row in range(rowBottom,rowTop-1,-1): + result.append(matrix[row][columnleft]) + + #columnleft needs to be incremented by 1 + columnleft+=1 + return result +sol = Solution() +print(sol.spiralOrder([[1,2,3],[4,5,6],[7,8,9]])) \ No newline at end of file