LeetCode 735. Asteroid Collision (Solution Explained)

  Рет қаралды 20,968

Nick White

Nick White

Күн бұрын

Пікірлер: 26
@pri18551
@pri18551 Жыл бұрын
your explanations are always so good.
@sachin_yt
@sachin_yt 4 жыл бұрын
wow! thank was refreshingly clear and concise! Thank you boss!
@kaikuregami2859
@kaikuregami2859 Жыл бұрын
Great explanation. thank you nick
@narayanbhat3279
@narayanbhat3279 4 жыл бұрын
Very well explained, it would be better if the while true loop is only for the last case, in all the other cases it just performs an operation and breaks
@lavanyam3224
@lavanyam3224 2 жыл бұрын
Not really, for this case: [-2, -2, 1, -2] after pushing the first three elements in stack, when you come for the last element, you would have to check all the three conditions again. If you've a solution where the while loop just runs for the last case, share here.
@z08840
@z08840 2 жыл бұрын
C++ in place, time O(n^2), space O(1) : 1. simplest, but lots of array moving: vector asteroidCollision(vector& a) { for(int left = 0, right = 1; right < a.size();) { if(a[left] > 0 && a[right] < 0) { int d = a[left] + a[right]; if(d = 0) a.erase(a.begin() + right); if(left < 0) ++left, ++right; } else ++left, ++right; } return a; } 2. efficient: vector asteroidCollision(vector& a) { for(int left = 0, right = 1; right < a.size();) { if(a[left] > 0 && a[right] < 0) { int d = a[left] + a[right]; if(d = 0 && a[left] == 0; --left); } if(d >= 0) { a[right] = 0; ++right; } if(left < 0) left = right++; } else left = right++; } a.resize(stable_partition(a.begin(), a.end(), [](int n){ return n != 0; }) - a.begin()); return a; }
@ahmedelsayed7762
@ahmedelsayed7762 4 жыл бұрын
+Nick White Please can you solve 1371? Its really hard and it involves Bit masks
@1murkeybadmayn
@1murkeybadmayn 8 ай бұрын
For the first example, input: [5, 10, -5], why is the output not [10] since 5 and -5 are moving in opposite directions and are on the correct sides to meet? Both should explode leaving 10 alone, no? I think they should have a constraint saying the asteroids have to be adjacent.
@EnterANameReal
@EnterANameReal 5 ай бұрын
5 and 10 don't collide 10 and -5 collide, which destroys -5 that leaves the first 5, 10 which are going in the same direction so they never collide
@pinesasyg9894
@pinesasyg9894 2 жыл бұрын
Well done.. amazing intuition
@wandered7
@wandered7 4 жыл бұрын
Thanks dude.. That helps a lot
@erajjoan4587
@erajjoan4587 Жыл бұрын
can do it without a while loop. just decrement the for loop's i when the top of the stack is positive and about to be destroyed by a greater negative
@god-speed03
@god-speed03 3 жыл бұрын
You could use while stack is not empty in inner while loop...
@ugola1
@ugola1 7 ай бұрын
Thank you, amazing explaination.
@damodaranm3044
@damodaranm3044 4 жыл бұрын
really well explained . in future videos pls do explain like this and dont pls rush up explain solwly . because begineers like us may get trouble in understanding your fast pace.
@thewatcherlollol
@thewatcherlollol 4 жыл бұрын
If you're a beginner you shouldn't be doing mediums..
@shivraj8033
@shivraj8033 5 ай бұрын
thns for this solution been puzzling me for the whole day
@chetanthapa4003
@chetanthapa4003 4 жыл бұрын
Bro please make video on basic calculator 2 on leetcode
@rohanreddymelachervu3498
@rohanreddymelachervu3498 2 жыл бұрын
Very clear explanation :) Thank you!!
@vaibhavgaware709
@vaibhavgaware709 3 жыл бұрын
Thanks dude.... nice explnation im going to subscribe your channel.
@tibettenballs4962
@tibettenballs4962 Жыл бұрын
why not make it a heap?
@mayankarcturus
@mayankarcturus 3 жыл бұрын
Amazing explanation, this video should go right into the LC discuss section. 👍🏻
@AndresIpod1
@AndresIpod1 4 жыл бұрын
Is the time complexity O(n^2)?
@ramazanchasygov3886
@ramazanchasygov3886 4 жыл бұрын
O(n), In the worst case scenario it would be like O(2n) which is O(n). [1, 2, 3, -4] => 2n [1, 2, -3, 4] => 2(n-1) + 1 [1, -2, 3, 4] => 2(n-2) + 2 [-1, 2, 3, 4] => 2(n-3) + 3 [1, 2, 3, 4] => n
@michaeldang8189
@michaeldang8189 4 жыл бұрын
The question violates physics!! :)
LeetCode 763. Partition Labels (Solution Explained)
12:56
Nick White
Рет қаралды 16 М.
Asteroid Collision - Stack - Leetcode 735
11:59
NeetCode
Рет қаралды 46 М.
Je peux le faire
00:13
Daniil le Russe
Рет қаралды 9 МЛН
Incredible Dog Rescues Kittens from Bus - Inspiring Story #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 37 МЛН
LeetCode 48. Rotate Image (Solution Explained)
10:18
Nick White
Рет қаралды 85 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 117 М.
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1 МЛН
Easy Google Coding Interview With Ben Awad
28:00
Clément Mihailescu
Рет қаралды 1 МЛН
LeetCode 525. Contiguous Array (Algorithm Explained)
9:54
Nick White
Рет қаралды 37 М.
LeetCode Open the Lock Solution Explained - Java
15:35
Nick White
Рет қаралды 21 М.
Google Coding Interview With A Competitive Programmer
54:17
Clément Mihailescu
Рет қаралды 2,5 МЛН
LeetCode Decode String Solution Explained - Java
10:52
Nick White
Рет қаралды 58 М.