diff --git a/container.py b/container.py new file mode 100644 index 00000000..dbb6ead1 --- /dev/null +++ b/container.py @@ -0,0 +1,32 @@ + +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: Use two pointers at the start and end of the array to compute area formed by the lines. +# Move the pointer with the smaller height to potentially find a larger area, updating the maximum each step. + + + + + + +class Solution: + def maxArea(self, height: List[int]) -> int: + left = 0 + right = len(height) - 1 + res = 0 + while left < right: + if height[left] > height[right]: + curr_amt = (right - left) * height[right] + right -=1 + else: + curr_amt = (right - left) * height[left] + left += 1 + res = max(res,curr_amt) + + return res + + + + diff --git a/sortColors.py b/sortColors.py new file mode 100644 index 00000000..2e92d148 --- /dev/null +++ b/sortColors.py @@ -0,0 +1,32 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: Use three pointers, l tracks position for next 0, h tracks position for next 2, m scans the array. + + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + l = 0 + m = 0 + h = len(nums) - 1 + + while m <= h: + + if nums[m] == 2: + temp = nums[m] + nums[m] = nums[h] + nums[h] = temp + h -= 1 + elif nums[m] == 0: + temp = nums[m] + nums[m] = nums[l] + nums[l] = temp + l += 1 + m += 1 + else: + m += 1 + \ No newline at end of file diff --git a/threeSum.py b/threeSum.py new file mode 100644 index 00000000..f3fa56eb --- /dev/null +++ b/threeSum.py @@ -0,0 +1,33 @@ +# Time Complexity : O(n²) +# Space Complexity : O(1) (excluding output) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach: Sort the array, fix one element, and use two pointers to find pairs whose sum with the fixed element equals zero. +# Move pointers based on the sum and skip duplicates to ensure only unique triplets are added. + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + res = [] + nums.sort() + for i in range(len(nums)): + if i != 0 and nums[i] == nums[i - 1]: + continue + j = i + 1 + k = len(nums) - 1 + while j < k: + if nums[i] + nums[j] + nums[k] == 0: + res.append([nums[i],nums[j],nums[k]]) + j += 1 + k -= 1 + while j < len(nums) - 1 and nums[j] == nums[j - 1]: + j += 1 + + while j < k and nums[k] == nums[k + 1]: + k -= 1 + elif nums[i] + nums[j] + nums[k] > 0: + k -= 1 + else: + j += 1 + return res + + \ No newline at end of file