wow! thank was refreshingly clear and concise! Thank you boss!
@kaikuregami2859 Жыл бұрын
Great explanation. thank you nick
@narayanbhat32794 жыл бұрын
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
@lavanyam32242 жыл бұрын
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.
@z088402 жыл бұрын
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; }
@ahmedelsayed77624 жыл бұрын
+Nick White Please can you solve 1371? Its really hard and it involves Bit masks
@1murkeybadmayn8 ай бұрын
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.
@EnterANameReal5 ай бұрын
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
@pinesasyg98942 жыл бұрын
Well done.. amazing intuition
@wandered74 жыл бұрын
Thanks dude.. That helps a lot
@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-speed033 жыл бұрын
You could use while stack is not empty in inner while loop...
@ugola17 ай бұрын
Thank you, amazing explaination.
@damodaranm30444 жыл бұрын
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.
@thewatcherlollol4 жыл бұрын
If you're a beginner you shouldn't be doing mediums..
@shivraj80335 ай бұрын
thns for this solution been puzzling me for the whole day
@chetanthapa40034 жыл бұрын
Bro please make video on basic calculator 2 on leetcode
@rohanreddymelachervu34982 жыл бұрын
Very clear explanation :) Thank you!!
@vaibhavgaware7093 жыл бұрын
Thanks dude.... nice explnation im going to subscribe your channel.
@tibettenballs4962 Жыл бұрын
why not make it a heap?
@mayankarcturus3 жыл бұрын
Amazing explanation, this video should go right into the LC discuss section. 👍🏻
@AndresIpod14 жыл бұрын
Is the time complexity O(n^2)?
@ramazanchasygov38864 жыл бұрын
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