From 6e9c8e95164463b566ba2ecde5c0660c35f93a52 Mon Sep 17 00:00:00 2001 From: Adithya Vinayak Date: Sat, 21 Feb 2026 22:04:51 -0500 Subject: [PATCH] working solution for all 3 problems --- problem1.py | 22 ++++++++++++++++++++++ problem2.py | 29 +++++++++++++++++++++++++++++ problem3.py | 15 +++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py create mode 100644 problem3.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..578bb0db --- /dev/null +++ b/problem1.py @@ -0,0 +1,22 @@ +# problem 1 + +class Solution: + def sortColors(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + start_ptr = 0 + end_ptr = len(nums)-1 + mid_ptr = 0 + while(mid_ptr<=end_ptr): + if nums[mid_ptr] == 1: + mid_ptr+=1 + elif nums[mid_ptr] == 0: + nums[mid_ptr],nums[start_ptr] = nums[start_ptr],nums[mid_ptr] + start_ptr+=1 + mid_ptr+=1 + else: + nums[mid_ptr],nums[end_ptr] = nums[end_ptr],nums[mid_ptr] + end_ptr-=1 + + \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..5c47cd26 --- /dev/null +++ b/problem2.py @@ -0,0 +1,29 @@ +# problem 2 + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + n = len(nums) + final_arr = [] + for i in range(n): + if i != 0 and nums[i] == nums[i-1]: + continue + if nums[i] > 0: + break + low = i+1 + high = n-1 + while low < high: + total = nums[i]+nums[low]+nums[high] + if total == 0: + final_arr.append([nums[i],nums[low],nums[high]]) + low+=1 + high-=1 + while low < high and nums[low-1] == nums[low]: + low+=1 + while low < high and nums[high] == nums[high+1]: + high-=1 + elif total > 0: + high -=1 + else: + low +=1 + return final_arr \ No newline at end of file diff --git a/problem3.py b/problem3.py new file mode 100644 index 00000000..7f34f55b --- /dev/null +++ b/problem3.py @@ -0,0 +1,15 @@ +# Problem 3 + +class Solution: + def maxArea(self, height: List[int]) -> int: + left_ptr,right_ptr = 0,len(height)-1 + curr_max = 0 + while(left_ptr!=right_ptr): + if height[left_ptr] > height[right_ptr]: + curr_area = height[right_ptr] * (right_ptr-left_ptr) + right_ptr -=1 + else: + curr_area = height[left_ptr] * (right_ptr-left_ptr) + left_ptr +=1 + curr_max = max(curr_area,curr_max) + return curr_max \ No newline at end of file