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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Two-Pointers-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions ContainerWithMostWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class ContainerWithMostWater {
public static void main(String args[]){
//int [] arr = {1,8,6,2,5,4,8,3,7};
int [] arr ={2,5,3,8,10,6,12,10};
System.out.println(maxArea(arr));
}

public static int maxArea(int[] height) {
/*
//Brute-force approach
int n = height.length;
int maxArea = 0;
for(int i =0; i<n; i++){ //O(n)
for(int j=i+1; j<n; j++){ //O(n) --> total time complexity O(n^2)
int currArea = Math.min(height[i], height[j]) * (j-i);
maxArea = Math.max(maxArea,currArea);
}
}
*/

//two-pointer approach
int n = height.length;
int maxArea = 0;
int left = 0;
int right = n-1;
while(left<right){ //O(n)
int currArea = Math.min(height[left],height[right]) * (right - left);
maxArea = Math.max(maxArea, currArea);
if(height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}

}

//Time complexity : O(n)
//Space complexity : O(1)
38 changes: 38 additions & 0 deletions SortColors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Arrays;
public class SortColors {
public void sortColors(int[] nums) {
int n = nums.length;
int left = 0;
int right = n-1;
int mid = 0;
while(mid <= right){ //O(n)
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 i, int j){
int temp = nums[i];
nums[j] = nums[i];
nums[i] = temp;
}

public static void main(String[] args) {
int[] arr2 = {0,0,2,1,1,0,0,1,2,2};

SortColors sc = new SortColors();
sc.sortColors(arr2);

System.out.println(Arrays.toString(arr2));
}
}

//Time complexity : O(n)
//Space complexity : O(1)
57 changes: 57 additions & 0 deletions ThreeSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.*;

public class ThreeSum {
public static List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
//Condition to check if length of array less than 3 or if its empty
if (nums == null || n < 3) return new ArrayList<>();

//Step 1: Sort the elements in array
Arrays.sort(nums);
Set<List<Integer>> result = new HashSet<>();
//Step 2: Using Two-pointer approach
//Fix position of first element i.e. nums[i], traverse until < n-2 as we need triplets in end
for (int i = 0; i < n - 2; i++) { //0(n)
int l = i + 1;
int r = n - 1;
while (l < r) { //0(n)
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
//Add the set and move to find other triplets, increment left and decrement right
result.add(Arrays.asList(nums[i], nums[l], nums[r]));
l++;
r--;
} else if (sum < 0) {
l++;
} else {
r--;
}
}
}
return new ArrayList<>(result);
}
/*
//Brute-force approach
for(int i=0; i<n-2;i++){
for(int j=i+1;j<n-1;j++){
for(int k=j+1;k<n;k++){
int sum = nums[i] + nums[j] + nums[k];
if(sum==0){
result.add(Arrays.asList(nums[i],nums[j],nums[k]));
}
}
}
}

return new ArrayList<>(result);
}
*/

public static void main(String args[]){
int [] arr1 ={-1,0,1,2,-1,-4};
System.out.println(threeSum(arr1));
}
}

//Time complexity : 0(n^2)
//Space complexity : O(n)
3 changes: 3 additions & 0 deletions out/production/Two-Pointers-1/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions out/production/Two-Pointers-1/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Two-Pointers-1/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/Two-Pointers-1/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/Two-Pointers-1/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
8 changes: 8 additions & 0 deletions out/production/Two-Pointers-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Two-Pointers-1

## Problem1 (https://leetcode.com/problems/sort-colors/)

## Problem2 (https://leetcode.com/problems/3sum/)

## Problem3 (https://leetcode.com/problems/container-with-most-water/)

Binary file added out/production/Two-Pointers-1/SortColors.class
Binary file not shown.
Binary file added out/production/Two-Pointers-1/ThreeSum.class
Binary file not shown.