L11. Aestroid Collisions | Stack and Queue Playlist

  Рет қаралды 42,182

take U forward

take U forward

Күн бұрын

Find problem link, notes under Step 9: takeuforward.o...
Follow me on socials: linktr.ee/take...

Пікірлер: 80
@prathamsharma5190
@prathamsharma5190 4 ай бұрын
the first question that i made by myself without any help, getting more confidence day by day!!!!
@ugthesep5706
@ugthesep5706 6 ай бұрын
solved by my own in around 33 minutes. Was confused at the starting like how are the collision happening then read the description carefully and i got it
@subhajitdey135
@subhajitdey135 6 ай бұрын
Same
@avengergirl_0464
@avengergirl_0464 6 ай бұрын
Then provide code
@subhajitdey135
@subhajitdey135 6 ай бұрын
@@avengergirl_0464 vector asteroidCollision(int N, vector &arr) { // code here stackst; int n=N; for(int i=0;i=0 || (st.top()0)) st.push(arr[i]); else{ while(!st.empty() && st.top()>0 && arr[i]
@ugthesep5706
@ugthesep5706 6 ай бұрын
@@avengergirl_0464 class Solution { public: vector asteroidCollision(vector& asteroids) { stack st; for(int i=0;i0 and asteroids[i]=top and top>0 and !st.empty()){ if(top==absval){ st.pop(); break; } st.pop(); if(!st.empty()) top = st.top(); } if(top!=absval and (asteroids[i]top)) st.push(asteroids[i]); } else st.push(asteroids[i]); } } vector res(st.size()); for(int i=res.size()-1;i>=0;i--){ res[i] = st.top(); st.pop(); } return res; } };
@MrBro-z7d
@MrBro-z7d 5 ай бұрын
@@avengergirl_0464 class Solution { public: vector asteroidCollision(vector& asteroids) { vector ans; int n=asteroids.size(); stack stk; stk.push(asteroids[0]); for(int i=1;i0) stk.push(asteroids[i]); else { while (!stk.empty() && stk.top() > 0 && stk.top() < abs(asteroids[i])) { stk.pop(); } if(!stk.empty() && asteroids[i]
@amanpreetsinghbawa1600
@amanpreetsinghbawa1600 Ай бұрын
Self solved, thanks again Striver for building up the intuitions in prev lectures🔥
@Akash-Bisariya
@Akash-Bisariya 5 ай бұрын
08:43 is very important to understand for an assumption that we need to insert everytime there is a positive element in array.
@Rohan-cn4ji
@Rohan-cn4ji 3 ай бұрын
thank you so much i literally was confused for straight up hours
@sankalpanand5099
@sankalpanand5099 28 күн бұрын
thank you!
@sankalpanand5099
@sankalpanand5099 28 күн бұрын
@@Rohan-cn4ji same :)
@agrawalmitesh4395
@agrawalmitesh4395 6 ай бұрын
no need to reverse the stack ,we have to return an array as answer , so we can get the stack size , create an array of that size and pop and directly start inserting into the array from backward direction(last index).
@akshitrajputhere
@akshitrajputhere 5 ай бұрын
Damn! Good observation
@randomshorts5200
@randomshorts5200 5 ай бұрын
that will take O(stack size) time, just use vector as stack.
@akshitrajputhere
@akshitrajputhere 5 ай бұрын
@@randomshorts5200 but is it a good practice?
@AyushEditz-hs6pf
@AyushEditz-hs6pf 4 ай бұрын
thats what we are trying to avoid.
@hashtagcc
@hashtagcc 6 ай бұрын
these type of question the bruteforce solution is the difficult one
@chirag71269
@chirag71269 Ай бұрын
THANK YOU STRIVER !
@Cubeone11
@Cubeone11 5 ай бұрын
i figured out the solution in just 5 minutes, pretty easy question if you could figure out that you have to use a stack.
@sahilmujawar8217
@sahilmujawar8217 3 ай бұрын
great explanation bhaiya
@babulalyadav4305
@babulalyadav4305 6 ай бұрын
00:04 Solving the problem of asteroid collisions in the given array. 02:20 Illustrates asteroid collisions and elimination process. 04:29 Using stack data structure to track element traversal 06:35 Asteroid collisions simulation using stack data structure 08:42 Using stack or list in asteroid collisions 10:47 Demonstration of asteroid collisions in a stack 12:59 Handling asteroid collisions using stack and queue 15:29 Explaining time and space complexity
@apmotivationakashparmar722
@apmotivationakashparmar722 4 ай бұрын
Thank you so much !
@UECAshutoshKumar
@UECAshutoshKumar 4 ай бұрын
Thank you
@SibiRanganathL
@SibiRanganathL 5 ай бұрын
Understood 👍
@oyeshxrme
@oyeshxrme 4 ай бұрын
thanks bhaiya
@DeadPoolx1712
@DeadPoolx1712 4 ай бұрын
UNDERSTOOD;
@mauryaToons
@mauryaToons 6 ай бұрын
We have to add one more condition in the last els if, and that is when the list.back()
@valendradangi1822
@valendradangi1822 6 ай бұрын
this condition is written in else block therefore arr[i] is already < 0 why are you checking it again? so (st.empty || st.back() < 0) will suffice.
@aaryansj8016
@aaryansj8016 2 ай бұрын
Yep this will require
@DrawwithNavi
@DrawwithNavi 6 ай бұрын
solved this without watching the video in 15 mins in o(n) w
@15anshulkumar
@15anshulkumar 6 ай бұрын
You are so talented bro
@Rahul-kw6zf
@Rahul-kw6zf 5 ай бұрын
chaalak bro
@AyushEditz-hs6pf
@AyushEditz-hs6pf 4 ай бұрын
nice
@rohanbera6227
@rohanbera6227 6 ай бұрын
This is only for my understanding pls ignore. Each asteroid is travelling at same speed. We are traversing from left to right in an array so if asteroid travelling from left to right it means it would not collide at that particular of timeframe which is kind of equivalent to the index of an array and if asteroid is coming from right to left it would collide because we are traversing from left to right (if there ) So we need to take negative number into consideration at that time and see if any asteroid is coming from left to right and if it is coming then it collides but once the asteroid collides (which was coming from right to left) it exits our timeframe. eg :- -2, -1, 1, 2 -2 at index 0 comes from right to left it should collide but there is no asteroid coming from left so nothing collides -> -3 -1 at index 1 comes from right to left it should collide but there is no asteroid coming from left so no collision -> -2 1 at index 2 going from left to right it shouldn't collide because we are travelling from left to right -> 1 -> +1 2 at index 3 doesn't collide -> +2
@aryansingh665
@aryansingh665 5 ай бұрын
Most of the time i got the idea whats happening and solve it through brute force but unable to optimize it may be 3-4 out of 10 time able to do so.
@rishi.vakharia
@rishi.vakharia 2 ай бұрын
def sign(num): return num//abs(num) def asteroidCollision(arr): n = len(arr) i = 0 lst = [] while(i < n): # will process asteroid i if len(lst) > 0 and lst[-1] > 0 and arr[i] < 0: # collision will happen if abs(lst[-1]) == abs(arr[i]): # tie lst.pop() i += 1 elif abs(lst[-1]) > abs(arr[i]): # last wins i += 1 else: # opp wins lst.pop() else: # no collision lst.append(arr[i]) i += 1 return lst print(asteroidCollision([-2,-1,1,2]))
@akshaysingh235
@akshaysingh235 5 ай бұрын
class Solution { public: vector asteroidCollision(vector& asteroids) { stack st; for(int i = 0; i < asteroids.size(); i++) { bool flag = false; while(!st.empty() && asteroids[i] < 0 && st.top() > 0) { if (abs(asteroids[i]) > abs(st.top())) { st.pop(); } else if (abs(asteroids[i]) == abs(st.top())) { st.pop(); flag = true; break; } else { flag = true; break; } } if (!flag) { st.push(asteroids[i]); } } vector ans; while (!st.empty()) { ans.insert(ans.begin(), st.top()); st.pop(); } return ans; } };
@divangijain279
@divangijain279 15 күн бұрын
what if the stack contains a negative number and is not empty
@rutujashelke4208
@rutujashelke4208 5 ай бұрын
Understood
@shreyxnsh.14
@shreyxnsh.14 5 ай бұрын
CPP Code I came up with: class Solution { public: vector asteroidCollision(vector& asteroids) { stack st; for(const auto& it: asteroids){ bool exploded = false; while(!(st.empty()) && it 0){ if(abs(it) > abs(st.top())){ st.pop(); }else if(abs(it) == abs(st.top())){ st.pop(); exploded = true; break; }else{ exploded = true; break; } } if(!exploded){ st.push(it); } } vector ans(st.size()); for(int i=st.size()-1;i>=0;i--){ ans[i] = st.top(); st.pop(); } return ans; } };
@SoulFrmTitanic
@SoulFrmTitanic 5 ай бұрын
bro thats a great one!! Can you just tell me what was the intuition behind this beautiful approach?? Would be very helpful for me!! 🤝
@shreyxnsh.14
@shreyxnsh.14 5 ай бұрын
@@SoulFrmTitanic i dont remember much, just thought to keep removing elements from stack until the current asteroid is destroyed (if the asteroid is weaker than the previous coz otherwise just remove the current asteroid and break out)
@SoulFrmTitanic
@SoulFrmTitanic 5 ай бұрын
@@shreyxnsh.14 achaa , ok bhai
@siddhantdeora8339
@siddhantdeora8339 13 күн бұрын
What if a positive elements comes in the last then what we have to do?
@subee128
@subee128 6 ай бұрын
Thanks
@pBERA0_0
@pBERA0_0 8 күн бұрын
how did you code the part you wrote in blue color. i was stuck on that part and couldn't understand why and how to formulate the code so that it is working as intended you coded that without discussing the edge case [8,-8] & [-2,-2,1,-2] and still its working as intended. so are we supposed to memorise your code
@Shivi32590
@Shivi32590 6 ай бұрын
understood
@AyushRaj-rr1hc
@AyushRaj-rr1hc 6 ай бұрын
I have doubt with input -2 -1 1 2, what would be the output in leetcode expected output is the same as input
@AbhishekGupta-zf2sw
@AbhishekGupta-zf2sw 6 ай бұрын
Yes, as 1st two move in left (stack is empty so push) and then rest of the element are moving right, opposite direction, therefore no collision
@omkarshendge5438
@omkarshendge5438 6 ай бұрын
@@AbhishekGupta-zf2sw yup this!
@aryasharma69
@aryasharma69 4 ай бұрын
everything will get added in the stack
@aniketnarayan677
@aniketnarayan677 4 күн бұрын
just for the last condition , where we are inserting element instead of and operator user a or operator
@mathsworldbysuraj6278
@mathsworldbysuraj6278 2 ай бұрын
This is simple , TC - O(4N)~N , SC - O(2N) vector asteroidCollision(vector& asteroids) { int n=asteroids.size(); stack st; vector nums; for(int i=0;i0 && valabs(top)) val=val; else if(abs(val)
@steveservant
@steveservant 6 ай бұрын
class Solution { public int[] asteroidCollision(int[] asteroids) { Stack stack = new Stack(); for(int i=0;i0){ stack.push(asteroids[i]); } else { while(!stack.isEmpty()){ int top = stack.peek(); if(top=0;i--){ ansArray[i] = stack.pop(); } return ansArray; } } //
@ArnavChauhan-j4k
@ArnavChauhan-j4k Ай бұрын
we could just use a vector instead of a list
@ShauryaGoyal-y6g
@ShauryaGoyal-y6g 5 ай бұрын
sir notes upload krdo site par
@charchitagarwal589
@charchitagarwal589 4 ай бұрын
The implementation is hard for this problem
@umeshchauhan3877
@umeshchauhan3877 6 ай бұрын
😊
@cyanideyt9579
@cyanideyt9579 5 ай бұрын
Java Solution TC : O(2N), O(N) for traversing and another O(N) for pushing and popping at max 'N' elements onto the stack. SC : O(2N), O(N) is for using external list data structure and another O(N) for converting the list into array to return the answer. class Solution { public int[] asteroidCollision(int[] asteroids) { // List to store the resulting asteroids after collisions List list = new ArrayList(); // Loop through each asteroid in the array for (int i = 0; i < asteroids.length; i++) { // If the current asteroid is moving to the right (positive direction) if (asteroids[i] > 0) { // Add it directly to the list (no collision with left-moving asteroids) list.add(asteroids[i]); } // If the current asteroid is moving to the left (negative direction) else { // Check for collisions with right-moving asteroids in the list while (!list.isEmpty() && list.get(list.size() - 1) > 0 && list.get(list.size() - 1) < Math.abs(asteroids[i])) { // Remove the smaller right-moving asteroid since it collides and explodes list.remove(list.size() - 1); } // If the list is empty or the last asteroid in the list is also moving to the left, // or there are no more right-moving asteroids to collide with if (list.isEmpty() || list.get(list.size() - 1) < 0) { // Add the current left-moving asteroid to the list list.add(asteroids[i]); } // If the last asteroid in the list is the same size but moving in the opposite direction else if (list.get(list.size() - 1) == Math.abs(asteroids[i])) { // Both asteroids destroy each other (equal in magnitude), so remove the last one list.remove(list.size() - 1); } // If the current left-moving asteroid is smaller, it is destroyed by the larger right-moving asteroid, // and we do not add it to the list (handled implicitly by not adding it to the list). } } // Convert the List of remaining asteroids to an array to return as the result int[] result = new int[list.size()]; for (int i = 0; i < list.size(); i++) { result[i] = list.get(i); } return result; } }
@samiranroyy1700
@samiranroyy1700 5 ай бұрын
class Solution { public int[] asteroidCollision(int[] asteroids) { int n = asteroids.length; Stack st = new Stack(); for(int i=0;i0) { st.push(asteroids[i]); }else{ while(!st.isEmpty() && st.peek()>0 && st.peek()
@premkulkarni7578
@premkulkarni7578 Ай бұрын
Solved by myself without watching video : class Solution { public: vector asteroidCollision(vector& nums) { int n = nums.size(); stack st; vector ans; for (int i=0 ; i 0){ st.push(nums[i]); } if (i != n &&(st.empty() && nums[i] < 0 || (!st.empty() && st.top() < 0)) && f){ st.push(nums[i]); } } if (st.empty()){ return {}; } while (!st.empty()){ ans.push_back(st.top()); st.pop(); } reverse(ans.begin() , ans.end()); return ans; } }; TBH Got cooked by a ton of edge cases !
@Malayalam_learner
@Malayalam_learner 6 күн бұрын
As soon as I saw your comment, skipped watching the video trying on my own
@mathsworldbysuraj6278
@mathsworldbysuraj6278 2 ай бұрын
vector asteroidCollision(vector& asteroids) { int n=asteroids.size(); stack st; vector nums; for(int i=0;i0 && valabs(top)) val=val; else if(abs(val)
@saketjaiswal3431
@saketjaiswal3431 5 ай бұрын
koi check karke batao na kya error hai isme. test cases pass nahi ho rahe class Solution { public: vector asteroidCollision(vector& asteroids) { vector st; int n = asteroids.size(); for(int i = 0; i0) st.push_back(asteroids[i]); else{ while(!st.empty() && st.back()>0 && st.back()
@kartikrameshchavan4710
@kartikrameshchavan4710 5 ай бұрын
for loop should be from 0 to n
@rajitpal9274
@rajitpal9274 6 ай бұрын
what's the code of 3:05 (when -3 is getting eliminated) anyone please??
@no_1313
@no_1313 5 ай бұрын
Cz there is 7 before which is a positive and greater than absolute of -3 i.e 3
@subhajitdey135
@subhajitdey135 6 ай бұрын
C++ solution with stack : vector asteroidCollision(int N, vector &arr) { // code here stackst; int n=N; for(int i=0;i=0 || (st.top()0)) st.push(arr[i]); else{ while(!st.empty() && st.top()>0 && arr[i]
@himanshugupta8430
@himanshugupta8430 6 ай бұрын
can you explain in this case [-19,-18, 20] Why is the answer [-19,-18, 20] and not [20].
@sripooja2802
@sripooja2802 6 ай бұрын
Bcoz, 1st 2 elements are moving to the left and the last element is moving to the right. So they won't collide
@sai-cz9lm
@sai-cz9lm 5 ай бұрын
because -19 is going left and 20 is going right so they can never collide
@abhinavabhi3568
@abhinavabhi3568 Ай бұрын
Understood
@sujalthakkar2118
@sujalthakkar2118 3 ай бұрын
understood
L12. Largest Rectangle in Histogram | Stack and Queue Playlist
31:42
take U forward
Рет қаралды 69 М.
L14. Remove K Digits | Stack and Queue Playlist
15:29
take U forward
Рет қаралды 39 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Don’t Choose The Wrong Box 😱
00:41
Topper Guild
Рет қаралды 62 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 570 М.
C can do this too and it's faster than Python
2:09:48
Tsoding Daily
Рет қаралды 15 М.
DeepSeek-R1 Paper Review
56:13
JoonHo LEE
Рет қаралды 3,6 М.
15 Sorting Algorithms "In C" but they get worse and worse
28:30
Score Follower
Рет қаралды 65 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 72 МЛН
L18. Implement LRU Cache
24:56
take U forward
Рет қаралды 93 М.
L8. Trapping Rainwater | 2 Approaches | Stack and Queue Playlist
28:58
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 2 МЛН
How to Hide a Wii (U) in a Computer
58:43
Bringus Studios
Рет қаралды 176 М.