HackerRank - Equal Stacks | Full Solution with Examples and Visuals

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

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 54
@the_shridhar
@the_shridhar 4 жыл бұрын
Next level explanation bhai! 🔥🔥🔥
@nikoo28
@nikoo28 4 жыл бұрын
Thank you so much. Please share the channel if you can. I can then create more and more such videos.
@sagarray707
@sagarray707 2 жыл бұрын
The explanation is Next level..... it's not less then paid course...
@nidhiawasthi4643
@nidhiawasthi4643 4 жыл бұрын
Beautifully explained....Thanku ❤
@nikoo28
@nikoo28 4 жыл бұрын
My pleasure 😊
@raviashwin1157
@raviashwin1157 4 жыл бұрын
Lovely explanation 😍
@nikoo28
@nikoo28 4 жыл бұрын
Thanks a lot 😊 Please share this video if possible, so that it can help others as well.
@raviashwin1157
@raviashwin1157 4 жыл бұрын
@@nikoo28 YEAH SURE!🙂
@GunaSeelan-zp5wj
@GunaSeelan-zp5wj 8 ай бұрын
Great Explanation
@nayabsamar9944
@nayabsamar9944 2 жыл бұрын
great explanation Sir. Can we improve the time complexity in any way? if we have 100 stacks then what solution we can use?
@khushiagarwal6601
@khushiagarwal6601 Жыл бұрын
best explanation!,thanks
@69_b2_waghmode_vishal2
@69_b2_waghmode_vishal2 3 жыл бұрын
🔥🔥🔥🔥 nyc explaination
@kadamsatyajit
@kadamsatyajit Жыл бұрын
G8 explaination. Thanks sir
@nikoo28
@nikoo28 Жыл бұрын
Always welcome
@unemployedcse3514
@unemployedcse3514 8 ай бұрын
good problem ❤
@ombohare6485
@ombohare6485 3 жыл бұрын
Great.. 👌👌
@bhavanasinghnolan
@bhavanasinghnolan 4 жыл бұрын
Really helpful.
@madhusudanchunletia7459
@madhusudanchunletia7459 4 жыл бұрын
i tried doing this by calculating the total sum of each vector ,then reversing the vectors , total - vec.back() ; , and then poping the last element until all the total sums are equal , but it is failing many test cases .please help me understand my error.
@nikoo28
@nikoo28 4 жыл бұрын
Make sure that you are reading the input arrays in the correct order, and creating the stack in right way. The first number is the top cylinder height. Try debugging and see if the top element is the one you desire in failing test cases.
@madhusudanchunletia7459
@madhusudanchunletia7459 4 жыл бұрын
@@nikoo28 thanks for replying , i'll try debugging .
@rushabhkanhed5896
@rushabhkanhed5896 2 жыл бұрын
Hello sir, can we used st1.peek() function directly instead of storing it into stack1Height variable if we do so it is giving error. Please explain.
@nikoo28
@nikoo28 2 жыл бұрын
That is because when you do ‘pop’ operations, the stack will change and then the peek operations will return different values.
@siddhantsingh2937
@siddhantsingh2937 2 жыл бұрын
Hello sir ! It worke!! But, the doubt i am having is , I didn't make variables like stackHeight1, instead directly put condition inside if statement as st1.peek() and st2.peek() the tesrcases were not passing like this. Why this though? Can't we use peek() function directly into the conditional statement.
@nikoo28
@nikoo28 2 жыл бұрын
It is hard to understand your logic, DM me from the info section of the channel and maybe I can help then
@siddhantsingh2937
@siddhantsingh2937 2 жыл бұрын
Ok! For sure
@siddhantsingh2937
@siddhantsingh2937 2 жыл бұрын
//this is the code which I wrote// while(!a.isEmpty() && !b.isEmpty() && !c.isEmpty()){ if (a.peek() == b.peek() && b.peek() == c.peek()) { maxHeight = a.peek(); break; } if (a.peek() >= b.peek() && a.peek() >= c.peek()) { a.pop(); } else if (b.peek() >= a.peek() && b.peek() >= c.peek()) { b.pop(); } else if (c.peek() >= a.peek() && c.peek() >= b.peek()) { c.pop(); } } return maxHeight; } Here a,b,c are stacks in my case //**This is your code** while (!st1.isEmpty() && !st2.isEmpty() && !st3.isEmpty()) { int stack1Height = st1.peek(); int stack2Height = st2.peek(); int stack3Height = st3.peek(); // If all stacks are of same height, just return the height if (stack1Height == stack2Height && stack2Height == stack3Height) { maxHeight = st1.peek(); break; } // Else find the stack with maximum height and remove the block if (stack1Height >= stack2Height && stack1Height >= stack3Height) { st1.pop(); } else if (stack2Height >= stack1Height && stack2Height >= stack3Height) { st2.pop(); } else if (stack3Height >= stack1Height && stack3Height >= stack2Height) { st3.pop(); } } return maxHeight; } //** My code didn't work but yours work just because you made variables like stack1Height and stack2Height for peeking top integer of the stack instead i just put peek functions directly into if statement , why this ?
@Habesha_Media_network
@Habesha_Media_network 11 ай бұрын
Nice Explanation, but you assumed there will be atleast one condition that satisfy the problem. this solution only passes 5 test cases out of 31
@nikoo28
@nikoo28 11 ай бұрын
the solution passes all the test cases on HackerRank, have a look at the code on my github link (check video description)
@unemployedcse3514
@unemployedcse3514 4 ай бұрын
this solution won't work for random input array
@nikoo28
@nikoo28 4 ай бұрын
give me a sample test case
@محمدالجريتلي-و5ز
@محمدالجريتلي-و5ز 3 жыл бұрын
why is this statement wrong in some test cases if(stk1.peek()==stk2.peek() && stk1.peek()==stk3.peek())
@LVenkateshan
@LVenkateshan 3 жыл бұрын
Can u explain the algorithm for game of two stacks in hackerrank..
@nikoo28
@nikoo28 3 жыл бұрын
I have some videos in the pipeline. Will try to solve your problem as soon as I can.
@_DuongMinhLong
@_DuongMinhLong 7 ай бұрын
Hi sir, nice Explanation but I think we can solve this problem without Stack, this is my Solution. public static int equalStacks(List h1, List h2, List h3) { int sum1 = h1.Sum(); int sum2 = h2.Sum(); int sum3 = h3.Sum(); int i1 = 0, i2 = 0, i3 = 0; while (true) { if (i1 == h1.Count || i2 == h2.Count || i3 == h3.Count) { return 0; } if (sum1 == sum2 && sum2 == sum3) { return sum1; } if (sum1 >= sum2 && sum1 >= sum3) { sum1 -= h1[i1]; i1++; } else if (sum2 >= sum1 && sum2 >= sum3) { sum2 -= h2[i2]; i2++; } else { sum3 -= h3[i3]; i3++; } } } }
@aryan_bhattarai
@aryan_bhattarai Жыл бұрын
how do u even think of these solutions... damn
@raghavendrarai9367
@raghavendrarai9367 8 ай бұрын
max height is 7 in dry-run example, please check.
@bizzie14
@bizzie14 6 ай бұрын
You have to reverse the input lists to get the right answer or fill the stacks starting from the last element of the list.
@pedrinho-gh4fn
@pedrinho-gh4fn 2 жыл бұрын
my code is not passing in all tests, can you tell me why? int ans = 0; h1.Reverse(); h2.Reverse(); h3.Reverse(); if(h1.Count==0||h2.Count==0||h3.Count==0) return 0; for(int i = 1;i
@pritishpattnaik4674
@pritishpattnaik4674 4 жыл бұрын
bro i tried this code in c++ , but i got wrong testcases
@nikoo28
@nikoo28 4 жыл бұрын
Check my github code in the description. You can then try to mimic the corresponding actions in c++ as well. Let me know what problem are you facing. The most common mistake is to read the input in a reverse order.
@pritishpattnaik4674
@pritishpattnaik4674 4 жыл бұрын
@@nikoo28 bro i am getting the output as 0 instead of 5
@nikoo28
@nikoo28 4 жыл бұрын
Try to debug the code and print some values to analyse if your code is working as expected.
@mystudyspace5814
@mystudyspace5814 4 жыл бұрын
cool
@malebeauty
@malebeauty 3 жыл бұрын
god :)
@collegematerial5348
@collegematerial5348 3 жыл бұрын
Please provide solution in c language anybody about this cumulative sum
@soumenmondal683
@soumenmondal683 4 жыл бұрын
sir my 25 test case got wrong I will share the code if you want java 8 would you like to share your telegram or email
@nikoo28
@nikoo28 4 жыл бұрын
If you look at the video description, I have posted my version of the code along with the test cases. Have a look, and let me know if you face any difficulties. 👍
@soumenmondal683
@soumenmondal683 4 жыл бұрын
@@nikoo28 hi sir thanks for replying but I want to solve my code first as I don't want to see a solution until I do it once on my own, it would be great if you help me
@nikoo28
@nikoo28 4 жыл бұрын
@@soumenmondal683 it would be hard for me to actually debug your code and figure out the problem. I can guide you. Try using IntelliJ and debug with the failing test case. You will surely figure out what is the problem.
@soumenmondal683
@soumenmondal683 4 жыл бұрын
@@nikoo28 Thank you sir
@Zhouri
@Zhouri Жыл бұрын
Below is a C++ sol :
@Zhouri
@Zhouri Жыл бұрын
int equalStacks(vector h1, vector h2, vector h3) { stack s1, s2, s3; // Calculate the prefix sums for all three stacks in reverse order int sum1 = 0, sum2 = 0, sum3 = 0; for (int i = h1.size() - 1; i >= 0; i--) { sum1 += h1[i]; s1.push(sum1); } for (int i = h2.size() - 1; i >= 0; i--) { sum2 += h2[i]; s2.push(sum2); } for (int i = h3.size() - 1; i >= 0; i--) { sum3 += h3[i]; s3.push(sum3); } // Find the minimum height among the three stacks while (!(s1.empty() || s2.empty() || s3.empty() || (s1.top() == s2.top() && s2.top() == s3.top()))) { int min_height = min({s1.top(), s2.top(), s3.top()}); if (s1.top() > min_height) s1.pop(); if (s2.top() > min_height) s2.pop(); if (s3.top() > min_height) s3.pop(); } // Return the common height of the three stacks return (s1.empty() || s2.empty() || s3.empty()) ? 0 : s1.top(); }
@vaibhavvarshney4523
@vaibhavvarshney4523 Жыл бұрын
I am pasting here the failed test case with the help of code and example: Hoping you get the problem with your code. TreeMap freqMap = new TreeMap(); freqMap.put(4, 1); freqMap.put(6, 1); freqMap.put(1, 2); int[] result = new int[freqMap.size()]; int i = 0; for (Map.Entry entry : freqMap.entrySet()) { result[i++] = entry.getKey(); } System.out.println(Arrays.toString(result)); // o/p - [1,4,6] // but o/p should be [1,1,4,6]
@vaibhavvarshney4523
@vaibhavvarshney4523 Жыл бұрын
BTW i like the way you explain the problem statement and the solution.
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Equal Stacks HackerRank Solution | Data Structures | Stacks
14:40
JAVAAID - Coding Interview Preparation
Рет қаралды 22 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
135 - Equal Stacks | Stacks | Hackerrank Solution | Python
7:19
Hackers Realm
Рет қаралды 4,7 М.
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19