diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..7b13b851 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,28 @@ +# Time Complexity --> O(n) +# Space Complexity --> O(1) +# Approach --> Using mid pointer as a fast pointer that traverses the list and swaps with low or high pointer accordingly +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + n = len(nums) + def swap(i,j): + temp = nums[i] + nums[i] = nums[j] + nums[j] = temp + + low = 0 + mid = 0 + high = n-1 + while mid<=high: + if nums[mid]==2: + swap(mid,high) + high=high-1 + elif nums[mid]==0: + swap(mid,low) + low = low+1 + mid = mid+1 + else: + mid = mid+1 + diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..6e22c411 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,87 @@ +# Time Complexity --> O(n^2) +# Space Complexity --> O(1) +# Approach --> Traverse over the list for i and look out for the remaining 2 values using pointers. Since the list is sorted, based on the total sum of values we can move the low and high pointers accordingly +class Solution: + def threeSum(self, nums: list[int]) -> list[list[int]]: + nums.sort() + n = len(nums) + result = [] + + for i in range(n): + low = i+1 + high = n-1 + if i!=0 and nums[i]==nums[i-1]: + continue + while low0: + high = high - 1 + else: + low = low+1 + return result + + +''' +# Time Complexity --> O(n^2logn) +# Space Complxity --> O(1) +class Solution: + def threeSum(self, nums: list[int]) -> list[list[int]]: + + def helper(arr, low, high, target): + while low<=high: + mid = low + (high-low)//2 + if arr[mid]==target: + return mid + elif arr[mid]>target: + high = high-1 + else: + low = low+1 + return -1 + + n = len(nums) + re = [] + target = 0 + nums.sort() + + for i in range(len(nums)): + innertarget = target-nums[i] + if i!=0 and nums[i]==nums[i-1]: + continue + for j in range(i+1,len(nums)): + if j!=i+1 and nums[j]==nums[j-1]: + continue + t = innertarget-nums[j] + idx = helper(nums, j+1, n-1, t) + + if idx!=-1: + triplet = tuple(sorted([nums[i], nums[j], nums[idx]])) + re.append(triplet) + + return re +''' + +''' +# Time Complexity --> O(n^3) +# Space Complxity --> O(m) where m is the number of unique triplets +class Solution: + def threeSum(self, nums: list[int]) -> list[list[int]]: + + re = set() + for i in range(len(nums)): + for j in range(i+1, len(nums)): + for k in range(j+1, len(nums)): + if nums[i]+nums[j]+nums[k]==0: + triplet = tuple(sorted([nums[i],nums[j],nums[k]])) + re.add(triplet) + + return [list(i) for i in re] + +''' diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..e824d47e --- /dev/null +++ b/Problem3.py @@ -0,0 +1,29 @@ +#Time Complexity --> O(n) +#Space Complexity --> O(1) +class Solution: + def maxArea(self, height: List[int]) -> int: + n = len(height) + low = 0 + high = n-1 + result = 0 + while low O(n^2) +#Space Complexity --> O(1) +class Solution: + def maxArea(self, height: List[int]) -> int: + n = len(height) + result = 0 + for i in range(n): + for j in range(i+1, n): + result = max(result, (j-i)*min(height[i], height[j])) + return result +'''