diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..342429bd --- /dev/null +++ b/Problem1.py @@ -0,0 +1,18 @@ +#Problem 238. PRODUCT OF ARRAY EXCEPT SELF +# TIME COMPELXITY: O(N) where N denotes number of elements +# SPACE COMPLEXITY: O(1) given that we are returning the array as an output that we are initializing + +class Solution(object): + def productExceptSelf(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + ans=[1]*len(nums) + right=1 + for i in range(1,len(nums)): + ans[i]=ans[i-1]*nums[i-1] + for i in range(len(nums)-1,0,-1): + right=right*nums[i] + ans[i-1]=ans[i-1]*right + return ans \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..27a693c3 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,41 @@ +#Problem 498: DIAGONAL TRAVERS +# TIME COMPELXITY: O(M*N) where M denotes the row and N denotes the col +# SPACE COMPLEXITY: O(1) are we only use variables to track directions and positioning + +class Solution(object): + def findDiagonalOrder(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + m=len(mat) + n=len(mat[0]) + result=[] + row=0 + col=0 + up=True + + while len(result)