From e2ca28ca72b0722d57490cdc7f5a57be07563cca Mon Sep 17 00:00:00 2001 From: Anirudh Venkateshwaran Date: Sat, 16 May 2026 14:36:02 -0700 Subject: [PATCH] Solved Sort Colors, 3Sum and Container With Most Water --- Problem1.cs | 38 ++++++++++++++++++++++++++++++++++++++ Problem2.cs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.cs | 29 +++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 Problem1.cs create mode 100644 Problem2.cs create mode 100644 Problem3.cs diff --git a/Problem1.cs b/Problem1.cs new file mode 100644 index 00000000..9798b71f --- /dev/null +++ b/Problem1.cs @@ -0,0 +1,38 @@ +// Time Complexity : O(n) where n is the total length of nums array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +public class Solution { + public void SortColors(int[] nums) { + int slow = 0, mid = 0, fast = nums.Length - 1; + + while(mid <= fast) + { + if(nums[mid] == 2) + { + Swap(nums, mid, fast); + fast--; + } + + else if(nums[mid] == 0) + { + Swap(nums, slow, mid); + slow++; + mid++; + } + + else + { + mid++; + } + } + } + + public void Swap(int[] nums, int i, int j) + { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} \ No newline at end of file diff --git a/Problem2.cs b/Problem2.cs new file mode 100644 index 00000000..401e39c7 --- /dev/null +++ b/Problem2.cs @@ -0,0 +1,50 @@ +// Time Complexity : O(n) where n is the total length of nums array +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +public class Solution { + public IList> ThreeSum(int[] nums) { + Array.Sort(nums); + List> result = new(); + for(int i = 0; i < nums.Length - 2; i++) + { + if(i>0 && nums[i]==nums[i-1]) + { + continue; + } + + int first = nums[i]; + int second = i+1, third = nums.Length -1; + while(second < third) + { + if(nums[second] + nums[third] == -first) + { + List temp = new(); + temp.Add(first); + temp.Add(nums[second]); + temp.Add(nums[third]); + result.Add(temp); + while(second < third && nums[second] == nums[second+1]) + second++; + while(second < third && nums[third] == nums[third-1]) + third--; + second++; + third--; + } + + else if(nums[second] + nums[third] < -first) + { + second ++; + } + + else + { + third --; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/Problem3.cs b/Problem3.cs new file mode 100644 index 00000000..20cc112f --- /dev/null +++ b/Problem3.cs @@ -0,0 +1,29 @@ +// Time Complexity : O(n) where n is the total length of height array +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +public class Solution { + public int MaxArea(int[] height) { + int low = 0, high = height.Length - 1; + int maxArea = 0; + + while(low<=high) + { + int minHeight = Math.Min(height[low], height[high]); + maxArea = Math.Max(maxArea, minHeight * (high-low)); + + if(height[low] < height[high]) + { + low++; + } + + else + { + high--; + } + } + + return maxArea; + } +} \ No newline at end of file