From d2e249acd7041849a5ebf120bfa5999de24672b3 Mon Sep 17 00:00:00 2001 From: tmujje Date: Thu, 6 May 2021 00:54:59 -0400 Subject: [PATCH 1/2] Two-Pointers-1 solutions --- 3Sum.java | 44 +++++++++++++++++++++++++++++++++++++++++++ ColorSort.java | 36 +++++++++++++++++++++++++++++++++++ WaterInContainer.java | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 3Sum.java create mode 100644 ColorSort.java create mode 100644 WaterInContainer.java diff --git a/3Sum.java b/3Sum.java new file mode 100644 index 00000000..3308a33e --- /dev/null +++ b/3Sum.java @@ -0,0 +1,44 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n2) since we have nested loops and nO(logn) for sorting the array initially +//Space Complexity: O(1) since we are not taking any extra space except the resultList + +public List> threeSum(int[] nums) { + + List> resultList = new ArrayList<>(); + + Arrays.sort(nums); + + for(int i = 0; i < nums.length - 2; i++){ + + //skip duplicate values + 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) + { + resultList.add(List.of(nums[i], nums[left], nums[right])); + left++; + right--; + // continue if we have the same numbers in the consecutive positions + while(left < right && nums[left] == nums[left-1]){ + left++; + } + while(left < right && nums[right] == nums[right+1]){ + right--; + } + } else if(sum > 0){ + right--; + } + else{ + left++; + } + } + } + return resultList; + } \ No newline at end of file diff --git a/ColorSort.java b/ColorSort.java new file mode 100644 index 00000000..06a8a951 --- /dev/null +++ b/ColorSort.java @@ -0,0 +1,36 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the array +//Space Complexity: O(1) since we are not taking any extra space + + public void sortColors(int[] nums) { + + //[2,0,2,1,1,0] + int left = 0; + int right = nums.length-1; + int mid = 0; + + while(mid <= right){ + + if(nums[mid] == 0){ // swap elements in mid and left pointers as lowest element should be on the left + swap(nums,mid,left); + left++; + mid++; + } + else if(nums[mid] == 2){ // swap elements in mid and right pointers since highest element should be on the right + swap(nums, mid, right); + right--; + } + else{ + mid++; + } + } + } + + private void swap(int[] nums, int leftIndex, int rightIndex){ + + int temp = nums[leftIndex]; + nums[leftIndex] = nums[rightIndex]; + nums[rightIndex] = temp; + } diff --git a/WaterInContainer.java b/WaterInContainer.java new file mode 100644 index 00000000..b22bbe8c --- /dev/null +++ b/WaterInContainer.java @@ -0,0 +1,37 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the array +//Space Complexity: O(1) since we are not taking any extra space + +public int maxArea(int[] height) + { + int left = 0; + int right = height.length - 1; + int max = Integer.MIN_VALUE; + while(left < right){ + int width = right - left; + + //Width * height is the area, but width keeps on decreasing + // as we move the pointers, so the only ideal way of hitting + // that maximum area would be based on the better height.This + // dictates my pointer movement. The pointer at the minimum + // height moves left or right accordingly + + if(height[left] < height[right]) + { + max = Math.max(max, width * height[left]); + left++; + } + else if(height[right] < height[left]){ + max = Math.max(max, width * height[right]); + right--; + } + else{ + max = Math.max(max, width * height[right]); + left++; + right--; + } + } + return max; + } \ No newline at end of file From 544dc6441c970aff035c2d6ef2519e03a6da1576 Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Thu, 13 Aug 2026 00:53:52 -0400 Subject: [PATCH 2/2] Complete Two-Pointers-1 assignment --- 3Sum.java | 44 ---------------------------------- ColorSort.java | 36 ---------------------------- SortColors.java | 43 +++++++++++++++++++++++++++++++++ ThreeSum.java | 53 +++++++++++++++++++++++++++++++++++++++++ WaterInContainer.java | 37 ---------------------------- WaterWithContainer.java | 38 +++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 117 deletions(-) delete mode 100644 3Sum.java delete mode 100644 ColorSort.java create mode 100644 SortColors.java create mode 100644 ThreeSum.java delete mode 100644 WaterInContainer.java create mode 100644 WaterWithContainer.java diff --git a/3Sum.java b/3Sum.java deleted file mode 100644 index 3308a33e..00000000 --- a/3Sum.java +++ /dev/null @@ -1,44 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n2) since we have nested loops and nO(logn) for sorting the array initially -//Space Complexity: O(1) since we are not taking any extra space except the resultList - -public List> threeSum(int[] nums) { - - List> resultList = new ArrayList<>(); - - Arrays.sort(nums); - - for(int i = 0; i < nums.length - 2; i++){ - - //skip duplicate values - 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) - { - resultList.add(List.of(nums[i], nums[left], nums[right])); - left++; - right--; - // continue if we have the same numbers in the consecutive positions - while(left < right && nums[left] == nums[left-1]){ - left++; - } - while(left < right && nums[right] == nums[right+1]){ - right--; - } - } else if(sum > 0){ - right--; - } - else{ - left++; - } - } - } - return resultList; - } \ No newline at end of file diff --git a/ColorSort.java b/ColorSort.java deleted file mode 100644 index 06a8a951..00000000 --- a/ColorSort.java +++ /dev/null @@ -1,36 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are iterating through the array -//Space Complexity: O(1) since we are not taking any extra space - - public void sortColors(int[] nums) { - - //[2,0,2,1,1,0] - int left = 0; - int right = nums.length-1; - int mid = 0; - - while(mid <= right){ - - if(nums[mid] == 0){ // swap elements in mid and left pointers as lowest element should be on the left - swap(nums,mid,left); - left++; - mid++; - } - else if(nums[mid] == 2){ // swap elements in mid and right pointers since highest element should be on the right - swap(nums, mid, right); - right--; - } - else{ - mid++; - } - } - } - - private void swap(int[] nums, int leftIndex, int rightIndex){ - - int temp = nums[leftIndex]; - nums[leftIndex] = nums[rightIndex]; - nums[rightIndex] = temp; - } diff --git a/SortColors.java b/SortColors.java new file mode 100644 index 00000000..fbc431e9 --- /dev/null +++ b/SortColors.java @@ -0,0 +1,43 @@ +// Approach: Use 3 pointers, mid and left at index 0 and right at the last index. +// Considering that left pointer collects 0, right pointer collects 2 and mid pointer collects 1 +// swap the elements accordingly until high crosses mid pointer. + +//Time Complexity: O(n) +//Space Complexity: O(1) + +class SortColors { + public void sortColors(int[] nums) { + + // Validate the inputs + if (nums == null || nums.length == 0) return; + + // Initialize the left, mid and right pointers + int left = 0, mid = 0, right = nums.length-1; + + while (mid <= right) + { + if (nums[mid] == 2) // Swap right pointer element with mid element as right collects 2 + { + swap(nums, mid, right); + right--; + } + else if (nums[mid] == 0) // Swap left pointer element with mid element as left pointer collects 0 + { + swap(nums, mid, left); + mid++; + left++; + } + else{ + mid++; + } + } + } + + // Swap function to sort two elements in the given indices i and j + private 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/ThreeSum.java b/ThreeSum.java new file mode 100644 index 00000000..80003f26 --- /dev/null +++ b/ThreeSum.java @@ -0,0 +1,53 @@ +import java.util.*; + +//Approach: We need to fix one element in the array while search for other two elements within the right part of the array (using two pointers) that makes the overall sum as 0, +// where we copy the elements to the result list. If not, we move the left and right pointers accordingly. We continue this for all elements of the array thus making the +// time complexity n power 2 for these iterations and nlogn for sorting the array. + +//Time Complexity : O(n2) + n log(n) +//Space Complexity: O(1) +public class ThreeSum{ + public List> threeSum(int[] nums) { + + List> resultList = new ArrayList<>(); + + //Validate the inputs + if (nums == null || nums.length == 0) return resultList; + + Arrays.sort(nums); + + for(int i = 0; i < nums.length - 2; i++){ + + //Skip duplicate values + 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) // Elements found and so copy to resultList and move the right and left pointers + { + resultList.add(List.of(nums[i], nums[left], nums[right])); + left++; + right--; + + // Continue when we have the same numbers in the consecutive positions + while(left < right && nums[left] == nums[left-1]){ + left++; + } + while(left < right && nums[right] == nums[right+1]){ + right--; + } + } else if(sum > 0){ + right--; + } + else{ + left++; + } + } + } + return resultList; + } +} + diff --git a/WaterInContainer.java b/WaterInContainer.java deleted file mode 100644 index b22bbe8c..00000000 --- a/WaterInContainer.java +++ /dev/null @@ -1,37 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are iterating through the array -//Space Complexity: O(1) since we are not taking any extra space - -public int maxArea(int[] height) - { - int left = 0; - int right = height.length - 1; - int max = Integer.MIN_VALUE; - while(left < right){ - int width = right - left; - - //Width * height is the area, but width keeps on decreasing - // as we move the pointers, so the only ideal way of hitting - // that maximum area would be based on the better height.This - // dictates my pointer movement. The pointer at the minimum - // height moves left or right accordingly - - if(height[left] < height[right]) - { - max = Math.max(max, width * height[left]); - left++; - } - else if(height[right] < height[left]){ - max = Math.max(max, width * height[right]); - right--; - } - else{ - max = Math.max(max, width * height[right]); - left++; - right--; - } - } - return max; - } \ No newline at end of file diff --git a/WaterWithContainer.java b/WaterWithContainer.java new file mode 100644 index 00000000..6709a9e5 --- /dev/null +++ b/WaterWithContainer.java @@ -0,0 +1,38 @@ +//Approach: With left and right pointers initialized, we need to find the minimum value between the elements at these two locations +// and accordingly calculate the area, update the maxValue and move the pointer. + +//Time Complexity: O(n) +//Space Complexity: O(1) + +public class WaterWithContainer +{ + public int maxArea(int[] height) + { + //Validate the inputs + if (height == null || height.length == 0) return 0; + + //Initialize the variables + int left = 0; + int right = height.length-1; + int max = Integer.MIN_VALUE; + int area = 0; + + + while(left < right) + { + if(height[left] < height[right]) + { + area = height[left] * (right-left); + max = Math.max(area, max); + left++; + } + else + { + area = height[right] * (right-left); + max = Math.max(area, max); + right--; + } + } + return max; + } +} \ No newline at end of file