HackerRank - Game of Two Stacks | Full solution with visuals | Study Algorithms

  Рет қаралды 11,106

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 33
@gilbishkosma4593
@gilbishkosma4593 11 ай бұрын
Earlier I was trying to solve this using recursion, but getting timelimit exceed in it, thanks for the solution, this one worked for me and the code is easy to read also.
@nikoo28
@nikoo28 10 ай бұрын
I usually avoid recursion. It is so hard to understand and debug 😅
@ANJALIGUPTA-vq1cv
@ANJALIGUPTA-vq1cv 2 жыл бұрын
i thought the same greedy approach and after unlocking the test case i get to know what im doing wrong.Thanks for the perfect explanation
@nikoo28
@nikoo28 2 жыл бұрын
You are most welcome :)
@lautaroblasco
@lautaroblasco Жыл бұрын
Great video man, it really helped me to understand the problem.
@b9944236
@b9944236 11 ай бұрын
Nice explanation, thanks.
@miomydude
@miomydude 2 жыл бұрын
Very clear thought and explanation. Thank you very much.
@jjoelthomas
@jjoelthomas 10 ай бұрын
Amazing work. Keep it up!
@rishabmudliar558
@rishabmudliar558 3 жыл бұрын
Thanks for explaining the approach. Could you do a DP approach? I initially tried it out but I got timeout error and then applied memoization but then my solution got weird so I came to your video. Thanks anyways:)
@ahadkhan6630
@ahadkhan6630 2 жыл бұрын
Thank you so much ... We made the solution clear but as I can see this solution actually violates the functions of the stack you can see that in the second loop, there must be some other solution
@nikoo28
@nikoo28 2 жыл бұрын
What function of the stack feels violated to you? I can try to clarify.
@pranavdeshpande6385
@pranavdeshpande6385 Жыл бұрын
Great explaination!
@SomanathMikali-f7f
@SomanathMikali-f7f 8 ай бұрын
37:27 That Game of two stacks is failing for the large number of inputs, getting time exceed error, is it probably that we use recursion huge number of times?
@doubtee15428
@doubtee15428 3 жыл бұрын
Sir if maximum sum is 10 instead of 11 then according to your logic it requires 3 element from stack 1 But answer is same 4
@nikoo28
@nikoo28 3 жыл бұрын
Try doing a dry run of the code with your input arguments. The logic I provided will check for all combinations, you will always check for elements from second stack
@shubhadarajmane7323
@shubhadarajmane7323 3 жыл бұрын
Could you please explain Time and Space Complexity of efficient approach that you have used?
@nikoo28
@nikoo28 3 жыл бұрын
The time complexity would be O(m + n) where 'm' and 'n' are the length of two stacks. That is because it might be possible you have to iterate through all elements of both the stacks for a very large maxSum. The space complexity would be O(1), as you are not consuming any extra space.
@shubhadarajmane7323
@shubhadarajmane7323 3 жыл бұрын
Ok...Thanks
@dineshkinibailoor340
@dineshkinibailoor340 10 ай бұрын
@@nikoo28 arent you running while inside second for loop? so it should be n^2 right?
@gauravgupta1803
@gauravgupta1803 2 жыл бұрын
Thank you so much 💘
@ANJALIGUPTA-vq1cv
@ANJALIGUPTA-vq1cv 2 жыл бұрын
thank you sir
@siddhantjagdish2554
@siddhantjagdish2554 2 жыл бұрын
Thank you!
@nikoo28
@nikoo28 2 жыл бұрын
Always welcome
@sreepadhsandilya5854
@sreepadhsandilya5854 2 жыл бұрын
very helpful....thank you..
@nikoo28
@nikoo28 2 жыл бұрын
You're welcome!
@ramkrishnainmotion
@ramkrishnainmotion 3 жыл бұрын
can we use dynamic programming ?
@pranavmishra5516
@pranavmishra5516 3 жыл бұрын
sir do u take any classes
@nikoo28
@nikoo28 3 жыл бұрын
not yet...but can plan it sometime in the future.
@saarthjhaveri8716
@saarthjhaveri8716 3 жыл бұрын
whats the logic behind this?
@nikoo28
@nikoo28 3 жыл бұрын
Think like this, “you only need to use additional elements if using them will increase the number of elements used” So, the general idea is to use one full stack first. Now, try to use the second stack. Remove one element at a time from the first stack and try including elements from second stack.
@HarshitSharma-kk6yz
@HarshitSharma-kk6yz 3 жыл бұрын
Hello
@ramkrishnainmotion
@ramkrishnainmotion 3 жыл бұрын
3 5 105 100 4 2 6 6 6 6 6 please explain this example...! ig we have to do in perspective of both stack then return the maximum one
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24
amazing#devil #lilith #funny #shorts
00:15
Devil Lilith
Рет қаралды 18 МЛН
Motorbike Smashes Into Porsche! 😱
00:15
Caters Clips
Рет қаралды 23 МЛН
Happy birthday to you by Secret Vlog
00:12
Secret Vlog
Рет қаралды 6 МЛН
136 - Game of Two Stacks | Stacks | Hackerrank Solution | Python
10:25
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 425 М.
The Last Algorithms Course You'll Need by ThePrimeagen | Preview
16:44
Frontend Masters
Рет қаралды 324 М.
Data Structures: Queue With Two Stacks
7:12
HackerRank
Рет қаралды 100 М.
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 363 М.
Towers of Hanoi: A Complete Recursive Visualization
21:13
Reducible
Рет қаралды 487 М.
Real Man relocate to Remote Controlled Car 👨🏻➡️🚙🕹️ #builderc
00:24