diff --git a/Colors.java b/Colors.java new file mode 100644 index 00000000..ea520ed8 --- /dev/null +++ b/Colors.java @@ -0,0 +1,43 @@ +// Time Complexity :O(N) where N is the length of the input array, as we are traversing the array once. +// Space Complexity :o(1) as we are using constant extra space for the pointers and temporary variable for swapping. +// Did this code successfully run on Leetcode :YES +// Any problem you faced while coding this :no + + +// Your code here along with comments explaining your approach +// We will use three pointers, low, mid and high. +// The low pointer will keep track of the position where the next 0 should be placed, +// the mid pointer will traverse the array and the high pointer will keep track of the position where the next 2 should be placed. +// We will iterate through the array with the mid pointer and if we encounter a 0, +// we will swap it with the element at the low pointer and move both pointers forward. +// If we encounter a 2, we will swap it with the element at the high pointer and move the high pointer backward. +// If we encounter a 1, we will simply move the mid pointer forward. +class Solution { + + public void sortColors(int[] nums) { + + int low = 0, mid = 0, high = nums.length - 1; + + while (mid <= high) { + + if (nums[mid] == 2) { + swap(nums, mid, high); + high--; + } + else if (nums[mid] == 0) { + swap(nums, mid, low); + low++; + mid++; + } + else { + mid++; + } + } + } + + private void swap(int[] nums, int left, int right) { + int temp = nums[left]; + nums[left] = nums[right]; + nums[right] = temp; + } +} \ No newline at end of file diff --git a/Containerwithwater.java b/Containerwithwater.java new file mode 100644 index 00000000..34ef27ed --- /dev/null +++ b/Containerwithwater.java @@ -0,0 +1,33 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach +// We will use two pointers, one at the beginning and one at the end of the array. +// We will calculate the area formed by the two pointers and update the maximum area. +// We will then move the pointer that has the smaller height, as moving the pointer with the larger height will not increase the area. +// We will continue this process until the two pointers meet. +// This approach ensures that we are checking all possible pairs of lines while maintaining a linear time complexity. +class Solution { + public int maxArea(int[] height) { + + int left = 0, right = height.length - 1; + int maxArea = 0; + + while (left < right) { + + int area = Math.min(height[left], height[right]) * (right - left); + maxArea = Math.max(maxArea, area); + + if (height[left] <= height[right]) { + left++; + } else { + right--; + } + } + + return maxArea; + } +} \ No newline at end of file diff --git a/Threesum.java b/Threesum.java new file mode 100644 index 00000000..2f40820b --- /dev/null +++ b/Threesum.java @@ -0,0 +1,57 @@ +// Time Complexity :O(N Log N) due to sorting, O(N^2) for the two pointer approach +// Space Complexity :O(N) for the result list but O(1) if we don't consider the output space +// Did this code successfully run on Leetcode :Yes +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach +// We will first sort the array to make it easier to avoid duplicates and use the two pointer approach. +// We will iterate through the array and for each element, we will use two pointers to find pairs that sum up to the negative of the current element. +// If we find a triplet that sums up to zero, we will add it to the result list and move both pointers while skipping duplicates. +// If the sum is less than zero, we will move the left pointer to increase the sum, and if the sum is greater than zero, we will move the right pointer to decrease the sum. +// This approach ensures that we are checking all possible triplets while maintaining a time complexity of O(N^2) and space complexity of O(N) for the result list. +import java.util.*; + +class Solution { + public List> threeSum(int[] nums) { + + List> res = new ArrayList<>(); + Arrays.sort(nums); + + for (int target = 0; target < nums.length; target++) { + + // skip duplicates + if (target > 0 && nums[target] == nums[target - 1]) { + continue; + } + + int left = target + 1; + int right = nums.length - 1; + + while (left < right) { + + int sum = nums[target] + nums[left] + nums[right]; + + if (sum == 0) { + + res.add(Arrays.asList(nums[target], nums[left], nums[right])); + + // skip duplicates + while (left < right && nums[left] == nums[left + 1]) left++; + while (left < right && nums[right] == nums[right - 1]) right--; + + left++; + right--; + } + else if (sum < 0) { + left++; + } + else { + right--; + } + } + } + + return res; + } +} \ No newline at end of file