diff --git a/3Sum.java b/3Sum.java new file mode 100644 index 00000000..5a207ab3 --- /dev/null +++ b/3Sum.java @@ -0,0 +1,69 @@ +import java.util.*; + +// Set O(n^2) + O(nlogn) time, O(n) space +// class Solution { +// public List> threeSum(int[] nums) { +// Set> ans = new HashSet<>(); +// int n = nums.length; + +// for (int i = 0; i < n; i++) { +// int target = 0 - nums[i]; +// Set set = new HashSet<>(); + +// for (int j = i+1; j < n; j++) { +// int complement = target - nums[j]; +// if (set.contains(complement)) { +// List temp = Arrays.asList(nums[i], nums[j], complement); +// Collections.sort(temp); +// ans.add(temp); +// } +// set.add(nums[j]); +// } +// } +// return new ArrayList<>(ans); +// } +// } + +// Two pointers O(n^2) + O(nlogn) time, O(1) space +class Solution { + public List> threeSum(int[] nums) { + List> ans = new ArrayList<>(); + int n = nums.length; + Arrays.sort(nums); + + for (int i = 0; i < n; i++) { + + if (i > 0 && nums[i] == nums[i-1]) { + continue; // skip duplicates + } + if (nums[i] > 0) break; // since we wont find triplet, array is sorted + + int left = i+1; + int right = n-1; + + while (left < right) { + int threeSum = nums[i] + nums[left] + nums[right]; + + if (threeSum == 0) { + ans.add(Arrays.asList(nums[i], nums[left], nums[right])); + left++; + right--; + + while (left < right && nums[left] == nums[left-1]) { + left++; + } + while (left < right && nums[right] == nums[right+1]) { + right--; + } + } + else if (threeSum < 0) { + left++; + } + else { + right--; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/ContainerWithMostWater.java b/ContainerWithMostWater.java new file mode 100644 index 00000000..56d68a31 --- /dev/null +++ b/ContainerWithMostWater.java @@ -0,0 +1,25 @@ +// Two pointers O(n) time, O(1) space +class Solution { + public int maxArea(int[] height) { + int left = 0; + int right = height.length-1; + + int area = 0; + int maxArea = 0; + + for (int i = 0; i < height.length; i++) { + int width = right-left; + int effectiveHeight = Math.min(height[left], height[right]); // height is limited by min height between left and right + area = width * effectiveHeight; + maxArea = Math.max(maxArea, area); + + if (height[left] < height[right]) { + left++; + } + else { + right--; + } + } + return maxArea; + } +} \ No newline at end of file diff --git a/SortColors.java b/SortColors.java new file mode 100644 index 00000000..99cde9f8 --- /dev/null +++ b/SortColors.java @@ -0,0 +1,28 @@ +// Two pointers O(n) time, O(1) space +class Solution { + public void sortColors(int[] nums) { + int left = 0; // collects 0s (red) + int mid = 0; // collects 1s (white) + int right = nums.length-1; // collects 2s (blue) + + while (mid <= right) { + if (nums[mid] == 0) { + swap(mid, left, nums); + left++; + mid++; + } + else if (nums[mid] == 1) { + mid++; + } + else { + swap(mid, right, nums); + right--; + } + } + } + private void swap(int a, int b, int[] nums) { + int temp = nums[a]; + nums[a] = nums[b]; + nums[b] = temp; + } +} \ No newline at end of file