Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 3sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity :O(n^2)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : We sort the array to make it easier to avoid duplicates and use the two-pointer technique. For each element, it sets two pointers (left and right) and moves them toward each other to find pairs that sum with the current element to zero. It skips duplicate elements to ensure each triplet is unique. All valid triplets are added to a list, which is returned at the end.


class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if(nums == null || nums.length<3) new ArrayList<>();
Arrays.sort(nums);
List<List<Integer>> output = new ArrayList<>();


for(int i=0; i<nums.length-2; i++) {
if(nums[i] > 0) break;
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) {
output.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 (sum > 0) {
right--;
} else {
left++;
}
}
}
return output;
}
}
25 changes: 25 additions & 0 deletions container-with-most-water.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity :O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : We start with two pointers at the two ends of the array.
// At each step, we calculate the area formed by the two lines and update the maximum area.
// We move the pointer with the smaller height inward, since moving the taller one cannot increase the area.

class Solution {
public int maxArea(int[] height) {
int pointer1 = 0;
int pointer2 = height.length -1;
int maxArea = 0;
while(pointer1 != pointer2) {
int curArea = Math.min(height[pointer1], height[pointer2]) * (pointer2 - pointer1);
maxArea = Math.max(maxArea, curArea );
if(height[pointer1] < height[pointer2]) {
pointer1 +=1;
} else {
pointer2 -=1;
}

}
return maxArea;
}
}
31 changes: 31 additions & 0 deletions sort-colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time Complexity :O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : I take three pointers. One on the left, one on the right and another starting from left and traverse through the array. While traversing if it finds 2, then it swaps with the right pointer and if it finds 0 it swaps with left pointer. It traverse until it meets right pointer.


class Solution {
public void sortColors(int[] nums) {
int left = 0;
int mid = 0;
int right = nums.length -1;
while(mid <= right) {
if(nums[mid] == 2) {
swap(nums, mid, right);
right--;
} else if (nums[mid] ==0) {
swap(nums, mid, left);
left++;
mid++;
} else {
mid++;
}
}
}

private void swap(int[] nums, int pointer1, int pointer2) {
int temp = nums[pointer1];
nums[pointer1] = nums[pointer2];
nums[pointer2] = temp;
}
}