4SUM | Leetcode 18 | Live coding session

preview_player
Показать описание
Here is the solution to "4Sum" leetcode question. Hope you have a great time going through it.

Chapters
1) 0:00 Explaining the problem out loud
2) 1:10 Question walkthrough
3) 2:30 Algorithm
5) 8:00 Coding it up

For discussion/feedback

PS : Please increase the speed to 1.25X
Рекомендации по теме
Комментарии
Автор

Beats 90% and works well for the newly added test cases as well....
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list=new ArrayList<>();

if(nums.length==4){
&& nums[1] !=
List<Integer> l = new ArrayList<>();
l.add(nums[0]);
l.add(nums[1]);
l.add(nums[2]);
l.add(nums[3]);
list.add(l);
return list;
}
if(nums[0] == || nums[0] ==
return list;
}
}

Arrays.sort(nums);
for(int i=0;i<nums.length-3;i++){
if(i!=0 && nums[i]==nums[i-1])continue;

for(int j=i+1;j<nums.length-1;j++){
if(j!=i+1 && nums[j]==nums[j-1])continue;

int left=j+1, right=nums.length-1;
while(left<right){
int
if(sum<target) left++;
else if(sum>target) right--;
else{
List<Integer> combs=new ArrayList<>();
combs.add(nums[i]);
combs.add(nums[j]);
combs.add(nums[left]);
combs.add(nums[right]);

list.add(combs);

left++;
right--;

while(left<right &&
while(left<right &&
}
}
}
}
return list;
}
}

FaizanKhan-gfaizank
Автор

Passing all test cases of this problem is more satisfying than having an actual foursome. Thanks.

focminging
Автор

Leetcode added new test case for overflow..this code doesn't work...it will fail test case 283

mdpearehabib
Автор

Good explanation bro... I have started learning DSA deeply for a month but couldn't solve any problems on own... Do we need to practice more to solve problems of our own?

vigneshmanoharan
Автор

can you please explain how this code will work for
arr: 1, 1, 1, 1
target=4

santoshvarma
Автор

Amazing explanation but if array:
and target: -294967296
this isn't going to run...

anshumanshukla
welcome to shbcf.ru