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
69 changes: 69 additions & 0 deletions 3Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.*;

// Set O(n^2) + O(nlogn) time, O(n) space
// class Solution {
// public List<List<Integer>> threeSum(int[] nums) {
// Set<List<Integer>> ans = new HashSet<>();
// int n = nums.length;

// for (int i = 0; i < n; i++) {
// int target = 0 - nums[i];
// Set<Integer> set = new HashSet<>();

// for (int j = i+1; j < n; j++) {
// int complement = target - nums[j];
// if (set.contains(complement)) {
// List<Integer> 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<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> 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;
}
}
25 changes: 25 additions & 0 deletions ContainerWithMostWater.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions SortColors.java
Original file line number Diff line number Diff line change
@@ -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;
}
}