From 742c5102c26b8e7c5f16e36e5b0370e3e02288cc Mon Sep 17 00:00:00 2001 From: praxpk <23168694+praxpk@users.noreply.github.com> Date: Thu, 8 Jan 2026 00:50:15 -0500 Subject: [PATCH] Two-Pointers-1 --- 3sum.py | 41 +++++++++++++++++++++++++++++++++++++++++ container_water.py | 22 ++++++++++++++++++++++ dutchflag.py | 26 ++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 3sum.py create mode 100644 container_water.py create mode 100644 dutchflag.py diff --git a/3sum.py b/3sum.py new file mode 100644 index 00000000..7694ad6b --- /dev/null +++ b/3sum.py @@ -0,0 +1,41 @@ +""" +time - n^2(log(n)) +space - o(1) +we sort the array, we take three pointers, one main pointer that iterates through the array, another two where left starts +from main pointer +1 and right starts at the end. We take the complement as 0-nums[i] and using the left and right pointers +we find the two sum solution for the compliment. +""" + +from typing import List + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + print(nums) + result = [] + for i in range(len(nums)): + if nums[i] > 0: + break + if i > 0 and nums[i] == nums[i - 1]: + continue + two_sum = 0 - nums[i] + l = i + 1 + r = len(nums) - 1 + while l < r: + left = nums[l] + right = nums[r] + temp_sum = left + right + if temp_sum == two_sum: + result.append([nums[i], nums[l], nums[r]]) + while l < r and nums[l] == left: + l += 1 + while l < r and nums[r] == right: + r -= 1 + elif temp_sum > two_sum: + while l < r and nums[r] == right: + r -= 1 + else: + while l < r and nums[l] == left: + l += 1 + return result diff --git a/container_water.py b/container_water.py new file mode 100644 index 00000000..f3d5b83d --- /dev/null +++ b/container_water.py @@ -0,0 +1,22 @@ +""" +time - o(n) +space - o(1) +we use two pointers that start at the ends, we calculate the area by multiplying distance between two pointers and least height among +the two. We take a greedy approach by moving the smaller of the two heights +""" + +from typing import List + + +class Solution: + def maxArea(self, height: List[int]) -> int: + left = 0 + right = len(height) - 1 + maxArea = float("-inf") + while left < right: + maxArea = max(maxArea, min(height[left], height[right]) * (right - left)) + if height[left] <= height[right]: + left += 1 + else: + right -= 1 + return maxArea diff --git a/dutchflag.py b/dutchflag.py new file mode 100644 index 00000000..bad0d9e1 --- /dev/null +++ b/dutchflag.py @@ -0,0 +1,26 @@ +""" +time - o(n) +space - o(1) +we take three pointers, one to iterate and two as place holders, left and right are placeholder pointers that keep a track of 0s and 2s +as and when the third pointer encounters a 0 or 2, we swap it with the left and right pointers, if it comes across a one we move the pointers +""" + +from typing import List + + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + l, r, m = 0, len(nums) - 1, 0 + while m <= r: + if nums[m] == 0: + nums[l], nums[m] = nums[m], nums[l] + l += 1 + m += 1 + elif nums[m] == 2: + nums[m], nums[r] = nums[r], nums[m] + r -= 1 + else: + m += 1