diff --git a/Solutions.py b/Solutions.py new file mode 100644 index 00000000..12beb718 --- /dev/null +++ b/Solutions.py @@ -0,0 +1,128 @@ +# Problem 1 : https://leetcode.com/problems/product-of-array-except-self/ +# Time complexity : O(n) +# Space Complexity : O(1) + +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + arr = [1]* len(nums) + + ##running product + ## creating a running product from right + ## and then going from left to right and multiplying to the same product + ## because the product will be all the elements on the left and right + ## finally returning the arr + rp =1 + for i in range(len(nums)): + arr[i] = arr[i] * rp + rp = rp *nums[i] + + rp =1 + for i in range(len(nums)-1,-1,-1): + arr[i] = arr[i] * rp + rp = rp * nums[i] + + return arr + + +# Problem 2: https://leetcode.com/problems/diagonal-traverse/ +# Time complexity : O(n) +# Space Complexity : O(1) + +class Solution(object): + def findDiagonalOrder(self, matrix): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + rows = len(matrix) + cols = len(matrix[0]) + + ## we can either go up or down, introducing a flag to store the condition + ## and then identifying what will be the next r.c + ## + + r,c = 0,0 + direction = True ##up + res = [] + while r