diff --git a/Two-sum_C1/two-sum.py b/Two-sum_C1/two-sum.py new file mode 100644 index 0000000..85e018b --- /dev/null +++ b/Two-sum_C1/two-sum.py @@ -0,0 +1,15 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + res=[] + numSet={} + for i in range(len(nums)): + if(target-nums[i] in numSet): + res.append(i) + res.append(numSet[target-nums[i]]) + numSet.pop(target-nums[i]) + else: + numSet[nums[i]]=i + return res + + +# TC=> O(n) and SC => O(n) \ No newline at end of file diff --git a/best-time-to-buy-and-sell/best-time-to-buy-and-sell.py b/best-time-to-buy-and-sell/best-time-to-buy-and-sell.py new file mode 100644 index 0000000..a3f3175 --- /dev/null +++ b/best-time-to-buy-and-sell/best-time-to-buy-and-sell.py @@ -0,0 +1,14 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + l,r=0,1 + res=0 + + while(rprices[l]): + res=max(res,prices[r]-prices[l]) + else: + l=r + r+=1 + return res if res!=0 else 0 +# TC=o(n) +# SC=o(1) \ No newline at end of file diff --git a/contains-duplicate/contains-duplicate.py b/contains-duplicate/contains-duplicate.py new file mode 100644 index 0000000..29002e4 --- /dev/null +++ b/contains-duplicate/contains-duplicate.py @@ -0,0 +1,9 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + numSet=set() + for num in nums: + if num in numSet: + return True + else: + numSet.add(num) + return False \ No newline at end of file diff --git a/find-min-in-rotated-arry/solution.py b/find-min-in-rotated-arry/solution.py new file mode 100644 index 0000000..50516f4 --- /dev/null +++ b/find-min-in-rotated-arry/solution.py @@ -0,0 +1,18 @@ + +class Solution: + def findMin(self, nums: List[int]) -> int: + res=nums[0] + l,r=0,len(nums)-1 + while(l<=r): + if nums[l] < nums[r]: + res = min(res, nums[l]) + break + m=(r+l)//2 + res=min(res,nums[m]) + if(nums[l]<=nums[m]): + l=m+1 + else: + r=m-1 + return res + + \ No newline at end of file diff --git a/product-of-array-execpt-self/product-array-execpt-self.py b/product-of-array-execpt-self/product-array-execpt-self.py new file mode 100644 index 0000000..2747977 --- /dev/null +++ b/product-of-array-execpt-self/product-array-execpt-self.py @@ -0,0 +1,15 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + result=[1] *len(nums) + left = [1] *len(nums) + right = [1] *len(nums) + for i in range(1,len(nums)): + left[i]=nums[i-1]*left[i-1] + for j in range(len(nums)-2,-1,-1): + right[j] = nums[j+1]*right[j+1] + for k in range(len(nums)): + result[k]= left[k]*right[k] + + return result +# TC: o(n) +# SC: 3o(n) -> o(n) \ No newline at end of file diff --git a/search-in-rotated-sorted-array/search-in-rotated-sorted-arrya.py b/search-in-rotated-sorted-array/search-in-rotated-sorted-arrya.py new file mode 100644 index 0000000..82c338b --- /dev/null +++ b/search-in-rotated-sorted-array/search-in-rotated-sorted-arrya.py @@ -0,0 +1,21 @@ +class Solution: + def search(self, nums: List[int], target: int) -> int: + left,right=0,len(nums)-1 + while (left<=right): + mid=(right+left)//2 + if nums[mid]==target: + return mid + if nums[left]<=nums[mid]: + if nums[left] <=target int: + c=a+b + return c + +# TC: o(n) +# SC: o(n) \ No newline at end of file diff --git a/sum-of-product-array/solution.py b/sum-of-product-array/solution.py new file mode 100644 index 0000000..050a6db --- /dev/null +++ b/sum-of-product-array/solution.py @@ -0,0 +1,19 @@ +class Solution: + def maxProduct(self, arr: List[int]) -> int: + if not arr: + return 0 + + max_ending_here = arr[0] + min_ending_here = arr[0] + max_so_far = arr[0] + + for i in range(1, len(arr)): + if arr[i] < 0: + max_ending_here, min_ending_here = min_ending_here, max_ending_here + + max_ending_here = max(arr[i], max_ending_here * arr[i]) + min_ending_here = min(arr[i], min_ending_here * arr[i]) + + max_so_far = max(max_so_far, max_ending_here) + + return max_so_far \ No newline at end of file diff --git a/sum-of-subarray/sum-of-subarry.py b/sum-of-subarray/sum-of-subarry.py new file mode 100644 index 0000000..9069108 --- /dev/null +++ b/sum-of-subarray/sum-of-subarry.py @@ -0,0 +1,9 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + currSum=nums[0] + maxSum=nums[0] + + for i in range(1,len(nums)): + currSum=max(nums[i],currSum+nums[i]) + maxSum=max(currSum,maxSum) + return maxSum \ No newline at end of file