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
26 changes: 26 additions & 0 deletions Leetcode_11.java
Original file line number Diff line number Diff line change
@@ -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<high){
int currArea=Math.min(height[low],height[high])*(high-low);
area=Math.max(area,currArea);
if(height[low]<height[high]){
low++;
}else{
high--;
}
}

return area;

}
}
103 changes: 103 additions & 0 deletions Leetcode_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//way1:
//Hold one value; Perform hashing on remaing values to get two elements such that the sum of holded value and another two elements from hashing =0.
//tc: o(n^2); sc: o(n)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> ans = new HashSet<>();
List<String> ansStr=new ArrayList<>();
int n=nums.length;
for(int i=0;i<n;i++){
int req=0-nums[i];

helper(nums,i+1,req,ans,nums[i],ansStr);
}

return new ArrayList<>(ans);
}

private void helper(int[] nums,int i,int req,Set<List<Integer>> ans,int val,List<String> ansStr){
// Map<Integer,Integer> map=new HashMap<>();
Set<Integer> set=new HashSet<>();

for(int j=i;j<nums.length;j++){
if(set.contains(req-nums[j])){
List<Integer> 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<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);

int n=nums.length;

for(int i=0;i<n;i++){
if(i!=0 && nums[i]==nums[i-1]){
continue;
}

int l=i+1,h=n-1;

while(l<h){
int sum=nums[l]+nums[h]+nums[i];

if(sum==0){
List<Integer> 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<h && nums[l]==nums[l-1]){
l++;
}

while(l<h && nums[h]==nums[h+1]){
h--;
}
}else if(sum<0){
l++;
}else{
h--;
}
}
}

return ans;
}
}
36 changes: 36 additions & 0 deletions Leetcode_75.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Three pointers
//low - to store 0's
//high - to store 2's
//mid travel
//if nums[mid]==2 --> 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++;
}


}

}
}