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/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