diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..e7ee4e1b --- /dev/null +++ b/problem1.py @@ -0,0 +1,18 @@ +# problem 1 - product except self + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + final_arr = [0] * len(nums) + rp = 1 + temp = 1 + for i in range(len(nums)): + rp = temp*rp + final_arr[i] = rp + temp = nums[i] + rp = 1 + temp = 1 + for i in range(len(nums)-1,-1,-1): + rp = temp*rp + final_arr[i] = rp * final_arr[i] + temp = nums[i] + return final_arr \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..c1c6a280 --- /dev/null +++ b/problem2.py @@ -0,0 +1,32 @@ +#problem 2 + +class Solution: + def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: + m = len(mat) + n = len(mat[0]) + result = [0] * (m * n) + r = c = 0 + direction = True + for i in range(m*n): + result[i] = mat[r][c] + if direction: + if c == n-1: + r+=1 + direction = False + elif r == 0: + c+=1 + direction = False + else: + r-=1 + c+=1 + else: + if r == m-1: + c+=1 + direction = True + elif c == 0: + r+=1 + direction = True + else: + r+=1 + c-=1 + return result \ No newline at end of file diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..e96ea484 --- /dev/null +++ b/problem3.py @@ -0,0 +1,26 @@ +# problem 3 +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + m = len(matrix) + n = len(matrix[0]) + final_arr = [] + top = 0 + bottom = m-1 + left = 0 + right = n-1 + while(top<=bottom and left<=right): + for i in range(left,right+1): + final_arr.append(matrix[top][i]) + top+=1 + for i in range(top,bottom+1): + final_arr.append(matrix[i][right]) + right-=1 + if top<=bottom: + for i in range(right,left-1,-1): + final_arr.append(matrix[bottom][i]) + bottom-=1 + if left<=right: + for i in range(bottom,top-1,-1): + final_arr.append(matrix[i][left]) + left+=1 + return final_arr \ No newline at end of file