diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Two-Pointers-1.iml b/.idea/Two-Pointers-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/.idea/Two-Pointers-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..700c1aac --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ContainerWithMostWater.java b/ContainerWithMostWater.java new file mode 100644 index 00000000..5f43b648 --- /dev/null +++ b/ContainerWithMostWater.java @@ -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 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> 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> 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(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) \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/.gitignore b/out/production/Two-Pointers-1/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml b/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/Two-Pointers-1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml b/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..919ce1f1 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml b/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/misc.xml b/out/production/Two-Pointers-1/.idea/misc.xml new file mode 100644 index 00000000..f5bd2dfe --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/modules.xml b/out/production/Two-Pointers-1/.idea/modules.xml new file mode 100644 index 00000000..700c1aac --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/.idea/vcs.xml b/out/production/Two-Pointers-1/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/out/production/Two-Pointers-1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/Two-Pointers-1/ContainerWithMostWater.class b/out/production/Two-Pointers-1/ContainerWithMostWater.class new file mode 100644 index 00000000..11b1303a Binary files /dev/null and b/out/production/Two-Pointers-1/ContainerWithMostWater.class differ diff --git a/out/production/Two-Pointers-1/README.md b/out/production/Two-Pointers-1/README.md new file mode 100644 index 00000000..16da027c --- /dev/null +++ b/out/production/Two-Pointers-1/README.md @@ -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/) + diff --git a/out/production/Two-Pointers-1/SortColors.class b/out/production/Two-Pointers-1/SortColors.class new file mode 100644 index 00000000..51eea564 Binary files /dev/null and b/out/production/Two-Pointers-1/SortColors.class differ diff --git a/out/production/Two-Pointers-1/ThreeSum.class b/out/production/Two-Pointers-1/ThreeSum.class new file mode 100644 index 00000000..121cc583 Binary files /dev/null and b/out/production/Two-Pointers-1/ThreeSum.class differ