From 192694d2b9851194a0de793d4f46b4201a0c98c8 Mon Sep 17 00:00:00 2001 From: takudzwa Date: Fri, 20 Feb 2026 20:39:37 -0500 Subject: [PATCH] [sort colors && 3 sum && max container] --- Sample.java | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/Sample.java b/Sample.java index f5c45b5f..ac57db12 100644 --- a/Sample.java +++ b/Sample.java @@ -4,4 +4,103 @@ // Any problem you faced while coding this : -// Your code here along with comments explaining your approach \ No newline at end of file +// Your code here along with comments explaining your approach + +public class Sample { + // Time Complexity : O(n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : yes + public void sortColors(int[] nums) { + int low = 0, mid = 0, high = nums.length - 1; + while (mid <= high) { + if (nums[mid] == 0) { + // swap + int temp = nums[low]; + nums[low] = nums[mid]; + nums[mid] = temp; + mid++; + low++; + + } else if (nums[mid] == 1) { + mid++; + } else { + int temp = nums[high]; + nums[high] = nums[mid]; + nums[mid] = temp; + high--; + } + } + } + // Time Complexity : O(n^2) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : yes + + public List> threeSum(int[] nums) { + List> result = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < nums.length && nums[i] <= 0 ; i++) { + + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + int left = i + 1; + int right = nums.length - 1; + + while (left < right) { + int sum = nums[i] + nums[left] + nums[right]; + + if (sum == 0) { + result.add(Arrays.asList(nums[i], nums[left], nums[right])); + + while (left < right && nums[left] == nums[left + 1]) { + left++; + } + + while (left < right && nums[right] == nums[right - 1]) { + right--; + } + + left++; + right--; + } + else if (sum < 0) { + left++; + } + else { + right--; + } + } + } + + return result; + } + + // Time Complexity : O(n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : yes + public int maxArea(int[] height) { + int left = 0; + int right = height.length - 1; + int max = 0; + + while (left < right) { + int width = right - left; + int h = Math.min(height[left], height[right]); + int area = width * h; + + max = Math.max(max, area); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } + + +} \ No newline at end of file