diff --git a/Problem1_SortColors.py b/Problem1_SortColors.py new file mode 100644 index 00000000..e908d341 --- /dev/null +++ b/Problem1_SortColors.py @@ -0,0 +1,32 @@ +# Time Complexity : O(n) where n is the length of the array +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Use Dutch National Flag algorithm with three pointers (left, current, right). +# Left pointer tracks boundary of 0s, right pointer tracks boundary of 2s, current pointer traverses. +# Swap elements based on current value: 0 goes to left, 2 goes to right, 1 stays in middle. + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + left = 0 + current = 0 + right = len(nums) - 1 + + while current <= right: + if nums[current] == 0: + # Swap with left pointer and move both forward + nums[left], nums[current] = nums[current], nums[left] + left += 1 + current += 1 + elif nums[current] == 2: + # Swap with right pointer, only move right backward + nums[current], nums[right] = nums[right], nums[current] + right -= 1 + else: + # nums[current] == 1, just move current forward + current += 1 + diff --git a/Problem2_3Sum.py b/Problem2_3Sum.py new file mode 100644 index 00000000..f764063c --- /dev/null +++ b/Problem2_3Sum.py @@ -0,0 +1,45 @@ +# Time Complexity : O(n^2) where n is the length of the array +# Space Complexity : O(1) excluding the output array +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Approach: Sort the array first, then for each element, use two pointers to find pairs that sum to negative of current element. +# Skip duplicates at each level (outer loop, left pointer, right pointer) to avoid duplicate triplets. +# Move pointers based on sum comparison: if sum < 0, move left forward; if sum > 0, move right backward. + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + result = [] + n = len(nums) + + for i in range(n - 2): + # Skip duplicates for the first element + if i > 0 and nums[i] == nums[i - 1]: + continue + + left = i + 1 + right = n - 1 + + while left < right: + current_sum = nums[i] + nums[left] + nums[right] + + if current_sum == 0: + result.append([nums[i], nums[left], nums[right]]) + + # Skip duplicates for left pointer + while left < right and nums[left] == nums[left + 1]: + left += 1 + # Skip duplicates for right pointer + while left < right and nums[right] == nums[right - 1]: + right -= 1 + + left += 1 + right -= 1 + elif current_sum < 0: + left += 1 + else: + right -= 1 + + return result + diff --git a/Problem3_ContainerWithMostWater.py b/Problem3_ContainerWithMostWater.py new file mode 100644 index 00000000..7c41ffd5 --- /dev/null +++ b/Problem3_ContainerWithMostWater.py @@ -0,0 +1,29 @@ +# Time Complexity : O(n) where n is the length of the array +# 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 both ends of the array, calculate area using shorter height and distance between pointers. +# Move the pointer with smaller height inward, as moving the larger height pointer cannot increase the area. +# Track maximum area encountered during traversal. + +class Solution: + def maxArea(self, height: List[int]) -> int: + left = 0 + right = len(height) - 1 + max_area = 0 + + while left < right: + # Calculate current area + width = right - left + current_area = min(height[left], height[right]) * width + max_area = max(max_area, current_area) + + # Move pointer with smaller height + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area +