diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..91c38825 --- /dev/null +++ b/problem1.java @@ -0,0 +1,40 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just need to handle mid pointer properly. + + +// Your code here along with comments explaining your approach in three sentences only +// I use three pointers low, mid, high to place 0s to the left and 2s to the right. +// If nums[mid] is 0, swap with low and move both; if it is 2, swap with high and move high only. +// If nums[mid] is 1, just move mid, and this finishes sorting in one pass. + +class Solution { + public void sortColors(int[] nums) { + + int low = 0, mid = 0; + int high = nums.length - 1; + + while (mid <= high) { + + if (nums[mid] == 0) { + swap(nums, low, mid); + low++; + mid++; + } + else if (nums[mid] == 2) { + swap(nums, mid, high); + high--; + } + else { + mid++; + } + } + } + + private void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..12fc243c --- /dev/null +++ b/problem2.java @@ -0,0 +1,52 @@ +// Time Complexity : O(n^2) +// Space Complexity : O(1) (excluding output list) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Avoiding duplicates is the main tricky part. + + +// Your code here along with comments explaining your approach in three sentences only +// First I sort the array, then fix one number and use two pointers to find pairs that make sum zero. +// When the sum is too small I move left pointer, when too large I move right pointer, and when zero I store the triplet. +// I skip duplicates for i, left, and right so output does not repeat. + +import java.util.*; + +class Solution { + public List> threeSum(int[] nums) { + + Arrays.sort(nums); + List> res = new ArrayList<>(); + + for (int i = 0; i < nums.length; i++) { + + if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicate i + + int left = i + 1; + int right = nums.length - 1; + + while (left < right) { + + int sum = nums[i] + nums[left] + nums[right]; + + if (sum == 0) { + res.add(Arrays.asList(nums[i], nums[left], nums[right])); + left++; + right--; + + // skip duplicates on left + while (left < right && nums[left] == nums[left - 1]) left++; + + // skip duplicates on right + while (left < right && nums[right] == nums[right + 1]) right--; + + } else if (sum < 0) { + left++; + } else { + right--; + } + } + } + + return res; + } +} diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..767d19cf --- /dev/null +++ b/problem3.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just need to move the smaller height pointer. + + +// Your code here along with comments explaining your approach in three sentences only +// I use two pointers at start and end and calculate area using width and minimum height. +// To possibly get a bigger area, I always move the pointer with smaller height because that is the limiting factor. +// I keep updating max area until pointers cross. + +class Solution { + public int maxArea(int[] height) { + + int left = 0; + int right = height.length - 1; + int max = 0; + + while (left < right) { + + int h = Math.min(height[left], height[right]); + int w = right - left; + + max = Math.max(max, h * w); + + if (height[left] < height[right]) left++; + else right--; + } + + return max; + } +}