diff --git a/Leetcode_11.java b/Leetcode_11.java new file mode 100644 index 00000000..ed0f4992 --- /dev/null +++ b/Leetcode_11.java @@ -0,0 +1,26 @@ +//Highest area-- high height and high width +//high width can be obtained with distance between low and high pointer. +//so, start low from one side and high from another side to obtain max width +//Capability to store high water at that place will comes from low container. So, low container servees as height +//Area=width * height +//move container with low heigh +//o(m+n) ; sc: o(1) +class Solution { + public int maxArea(int[] height) { + int area=0; + int low=0,high=height.length-1; + + while(low> threeSum(int[] nums) { + Set> ans = new HashSet<>(); + List ansStr=new ArrayList<>(); + int n=nums.length; + for(int i=0;i(ans); + } + + private void helper(int[] nums,int i,int req,Set> ans,int val,List ansStr){ + // Map map=new HashMap<>(); + Set set=new HashSet<>(); + + for(int j=i;j curr=new ArrayList<>(); + int need=req-nums[j]; + + // StringBuilder sb=new StringBuilder(); + curr.add(val); + curr.add(nums[j]); + curr.add(req-nums[j]); + Collections.sort(curr); + + // for(int v:curr){ + // sb.append(v); + // sb.append(v); + // sb.append(v); + // } + // StringBuilder sb=new StringBuilder(); + + + // System.out.println(sb.toString()); + // System.out.println(curr); + + // if(!(ansStr.contains(sb.toString()))){ + // ans.add(curr); + // ansStr.add(sb.toString()); + // } + ans.add(curr); + }else{ + set.add(nums[j]); + } + } + } +} + +//way2: +//Sort arrays.. Hold one value and apply two pointer on remaining values to get desired sum +//tc: o(n^2); sc: o(1) +class Solution { + public List> threeSum(int[] nums) { + List> ans = new ArrayList<>(); + Arrays.sort(nums); + + int n=nums.length; + + for(int i=0;i curr=new ArrayList<>(); + //Array is sorted. Need not perform sorting again + curr.add(nums[i]); + curr.add(nums[l]); + curr.add(nums[h]); + ans.add(curr); + l++; + h--; + + while(l swap with high pointer +//if nums[mid]==0 --> swap with low pointer +//if nums[mid]==1, don't swap. Just increment pointer. If later 0 comes, then swap will happen. + +//tc: o(n), sc: o(1) +class Solution { + public void sortColors(int[] nums) { + int n=nums.length; + + int low=0,high=n-1,mid=0; + + while(mid<=high){ + if(nums[mid]==2){ + int temp=nums[high]; + nums[high]=nums[mid]; + nums[mid]=temp; + high--; + }else if(nums[mid]==0){ + int temp=nums[low]; + nums[low]=nums[mid]; + nums[mid]=temp; + low++; + mid++; + }else if(nums[mid]==1){ + mid++; + } + + + } + + } +}