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
24 changes: 24 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Problem1 {
public void sortColors(int[] nums) {
int low = 0, high = nums.length-1, mid = 0;

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 x, int y){
int temp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}
40 changes: 40 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Problem2 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int target = 0;
List<List<Integer>> result = new ArrayList<>();
for(int i=0; i<n; i++){

if(i != 0 && nums[i] == nums[i-1]) continue;

int innerTarget = target - nums[i];

for(int j=i+1; j<n; j++){

if(j != i+1 && nums[j] == nums[j-1]) continue;

int t = innerTarget - nums[j];
int idx = binarySearch(nums, j+1, nums.length-1, t);
if(idx != -1){
List<Integer> triplet = Arrays.asList(nums[i], nums[j], nums[idx]);
result.add(triplet);
}
}
}

return result;
}

private int binarySearch(int[] nums, int low, int high, int target){

while(low <= high){
int mid = low + (high - low)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] > target) high = mid - 1;
else low = mid + 1;
}

return -1;
}
}
25 changes: 25 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Problem3 {
public int maxArea(int[] height) {
int n = height.length;

int low = 0, high = n-1;
int area = 0;

while(low < high){
int h = 0;
int w = high - low;

if(height[low] < height[high]){
h = height[low];
low++;
}else{
h = height[high];
high--;
}

area = Math.max(area, h*w);
}

return area;
}
}