From db9b8ed8a4756ae38c7e15f0bcd258851e514833 Mon Sep 17 00:00:00 2001 From: Sarvesh Sawant Date: Wed, 24 Dec 2025 18:26:26 -0500 Subject: [PATCH] Solve all problems --- Problem1.java | 31 ++++++++++++++++++++++++++++ Problem2.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.java | 30 +++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java create mode 100644 Problem3.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..5d0edae5 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,31 @@ +/* +75. Sort Colors +Ran on leetcode: Yes +TC: O(n) SC: O(1) +Use 3 pointers, low and mid on first element of array and high on last element of array, traverse mid through array and if element on mid is 0 swap it with low and move low and mid pointer, if element is 2 swap between mid and high and move high pointer and if element is 1 it is already in correct position and move the mid pointer +*/ +class Solution { + public void sortColors(int[] nums) { + int low = 0, mid = 0, high = nums.length - 1; // Divide nums array in 3 sections + + while(mid <= high){ + if(nums[mid] == 0){ // Move the element with 0 value to low section + swap(nums, low, mid); // swap the elements + mid++; // Move mid, cauz the element is already at correct position, because we have seen it already + low++; // Move low pointer, to place next 0 value element + } else if(nums[mid] == 1){ // Correct position, move mid + mid++; + } else if(nums[mid] == 2){ // Move the element with 2 value to high section + swap(nums, high, mid); // swap the elements + high--; // Move high pointer, to place next 2 value element + } + } + + } + + private void swap(int[] nums, int i, int j){ // swap elements on index i and j + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..c0d24f64 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,57 @@ +/* +15. 3Sum +Ran on leetcode: Yes +TC: O(n^2) SC: O(1) +Sort the array, fix one pointer and apply two sum on the subarray to find the target. In two sum if the sum > 0 then move the second pointer to get lower value and if sum < 0 move the first pointer to get higher value item. Increment fixed pointer and apply two sum again. +*/ +class Solution { + public List> threeSum(int[] nums) { + + List> result = new ArrayList<>(); + + // Sort, to use two pointers to find the target + Arrays.sort(nums); + + // Fix first pointer and two pointers on rest of array + int low = 1; + int high = nums.length - 1; + + for (int i = 0; i < nums.length; i++) { // Fix one element + if (i != 0 && nums[i] == nums[i - 1]){ + continue; + } + if(nums[i] > 0) break; // Early termination if the element is positive, cauz the other two elements will be also positive and never sum to 0 + low = i + 1; // First pointer on the subarray + high = nums.length - 1; // Second pointer on last element + while (low < high) { // Find the target based on rest of the subarray + int sum = nums[low] + nums[high] + nums[i]; + if (sum == 0) { // If target is found add to result and move both pointers + result.add(List.of( + nums[low], + nums[high], + nums[i])); + low++; + high--; + while (low < high && nums[high] == nums[high + 1]) { // Skip duplicates + high--; + } + while (low < high && nums[low] == nums[low - 1]) { // Skip duplicates + low++; + } + } else if (sum > 0) { + high--; // If sum is more than target, move pointer to get lower value + while (low < high && nums[high] == nums[high + 1]) { // Skip duplicates + high--; + } + } else { + low++; // If sum is less than target, move pointer to get higher value + while (low < high && nums[low] == nums[low - 1]) { // Skip duplicates + low++; + } + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..498ab726 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,30 @@ +/* +11. Container With Most Water +Ran on leetcode: Yes +TC: O(n) SC: O(1) +Start with max width, low pointer on first element and high pointer on last element, if height on low pointer is smaller than on high pointer move the low pointer and vice versa, since height is the limiting factor and we already have the max width. At each point calculate area and update max if necessary. +*/ +class Solution { + public int maxArea(int[] height) { + // Set to max width + int low = 0, high = height.length - 1; + + int area = Integer.MIN_VALUE; + + while(low < high){ + + int width = high - low; // Find the distance + int minHeight = Math.min(height[low], height[high]); // Find the limiting height + area = Math.max(area, width * minHeight); // Store area, if it is new max + + if(height[low] < height[high]){ // height on low index is limiting + low++; + } else { // height on high index is limiting + high--; + } + + } + + return area; + } +} \ No newline at end of file