Very nice. I recently went through this myself and found myself always thinking that O(n^2) was always "bad" and couldnt be the right answer, but thats not the case always. Also helps once you know that this problem is just Two Sum inside an outer loop.
@AlgoJS
2 жыл бұрын
Very true, similar difference between 4Sum and 3Sum
@bidaarle
Ай бұрын
Great explanation dude
@andersontiban5356
Ай бұрын
thank you very clear and helpful!
@CoffeePoweredDev
Ай бұрын
Thanks for the explanation! Shouldn't line 26 be ``` while(nums[k] === nums[k-1]) k--; ``` Since we want to move our right pointer more to the left hence comparing to the element which comes before it?
@Endvvell
Жыл бұрын
Much better than all those 30 minute videos going all over the place with their explanation, yours is concise and straight to the point. Thank you! The only thing I didn't get is why, when preventing the K duplicates with the while loop you are comparing K to K+1. Shouldn't it be K === K-1 since we are decrementing K and so are moving down the sorted array?
@AlgoJS
Жыл бұрын
Thanks Svitanok, that's the aim of the channel - to keep it concise and to the point to save viewers time. With the while loop I think both could be used, it just means that initially K === K+1 is doing an extra step comparing K (the last value in the array) with a value that's undefined, good spot 👍
@zdzisawdyrman7789
2 ай бұрын
If k = nums.length-1 will this not explode when you try to access nums[k++] in the duplicates section ? Say nums = [-1,-1,0,2], then for i == 0, j == 1 and k == 3 we would access nums[k++] which would blow up. Unless, Javascript allows for it? I do not know Javascript.
@darshitgajjar5199
Жыл бұрын
Great Algo JS, Pls make more videos in Leetcode problems. You are doing a Great JOB! I subscribed to your channel, but need more content, especially in JS --> LeetCode PS.
@manmohanpatil
10 ай бұрын
Awsome explanation. Do we need to write nums[k-1] instead of nums[k+1] on line 26
@anujgupta153
6 ай бұрын
Yes was wondering this as well, it should be k-1 not k + 1 because k + 1 will never hit in the first instance as k is the end of the array, and in a case following the first instance, it will push the duplicate k before checking if nums[k] === nums[k + 1] again
@anujgupta153
6 ай бұрын
Also I believe since this check occurs in one iteration of the for loop for i, and we are checking for a duplicate j. This case will never occur since it can only occur if i and j are duplicates to get the same result. ie first answer is i = - 4, j = -1, k= 5 in an array [-4, -1, -1, 5,, 5,] then next iteration it will keep i = 4, j will increment to 5 for the duplicates and 5 wont be able to duplicate since the sum will never = 0
@talhacoder5084
5 ай бұрын
The reason it is k+1 instead of k-1 is that k is moving in reverse and the previous value of k would the one after it. Also an easier way to handle this without checking unique for k is that instead of comparing j with j+1 which would be the next value of j just check j with j-1 which is the previous value for it and then we would not have to check for k at all.
@martinlabastie3518
10 ай бұрын
You are awesome dude!
@bassilmohammad7266
Жыл бұрын
You are the beast !!!
@AlgoJS
Жыл бұрын
Thanks for the great comment! Keep on grinding Leetcode
@shrutiagrawal2664
4 ай бұрын
leet code is not accepting this code anymore, it is showing wrong answer
@AlgoJS
4 ай бұрын
Please review your code, the answer is still accepted!!
@amotekundev
10 ай бұрын
Thank you
@raboyusuf2323
2 жыл бұрын
Thanks Man
@andrewkuhlman1665
Жыл бұрын
How does O( n log(n) * n^2) simplify to n^2?
@samudraperera6546
11 ай бұрын
I think its because N^2 is the largest growing value when N approached infinity so we can discount n log n and just refer to the time complexity as N^2
@mohamedassalladicko7290
Ай бұрын
It is not times (nlogn * n^2), but rather an addition what I mean is : nlogn + n^2 = n^2
Пікірлер: 23