Тёмный

HackerRank - Equal Stacks | Full Solution with Examples and Visuals 

Nikhil Lohia
Подписаться 45 тыс.
Просмотров 10 тыс.
50% 1

Опубликовано:

 

21 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 52   
@the_shridhar
@the_shridhar 3 года назад
Next level explanation bhai! 🔥🔥🔥
@nikoo28
@nikoo28 3 года назад
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...
@GunaSeelan-zp5wj
@GunaSeelan-zp5wj 3 месяца назад
Great Explanation
@nidhiawasthi4643
@nidhiawasthi4643 3 года назад
Beautifully explained....Thanku ❤
@nikoo28
@nikoo28 3 года назад
My pleasure 😊
@unemployedcse3514
@unemployedcse3514 3 месяца назад
good problem ❤
@raviashwin1157
@raviashwin1157 3 года назад
Lovely explanation 😍
@nikoo28
@nikoo28 3 года назад
Thanks a lot 😊 Please share this video if possible, so that it can help others as well.
@raviashwin1157
@raviashwin1157 3 года назад
@@nikoo28 YEAH SURE!🙂
@khushiagarwal6601
@khushiagarwal6601 Год назад
best explanation!,thanks
@madhusudanchunletia7459
@madhusudanchunletia7459 3 года назад
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 3 года назад
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 3 года назад
@@nikoo28 thanks for replying , i'll try debugging .
@kadamsatyajit
@kadamsatyajit Год назад
G8 explaination. Thanks sir
@nikoo28
@nikoo28 Год назад
Always welcome
@69_b2_waghmode_vishal2
@69_b2_waghmode_vishal2 3 года назад
🔥🔥🔥🔥 nyc explaination
@bhavanasinghnolan
@bhavanasinghnolan 3 года назад
Really helpful.
@_DuongMinhLong
@_DuongMinhLong Месяц назад
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++; } } } }
@nayabsamar9944
@nayabsamar9944 Год назад
great explanation Sir. Can we improve the time complexity in any way? if we have 100 stacks then what solution we can use?
@ombohare6485
@ombohare6485 3 года назад
Great.. 👌👌
@Habesha_Media_network
@Habesha_Media_network 6 месяцев назад
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 6 месяцев назад
the solution passes all the test cases on HackerRank, have a look at the code on my github link (check video description)
@user-qd8cm9hh7u
@user-qd8cm9hh7u 3 года назад
why is this statement wrong in some test cases if(stk1.peek()==stk2.peek() && stk1.peek()==stk3.peek())
@aryanbhattarai6188
@aryanbhattarai6188 10 месяцев назад
how do u even think of these solutions... damn
@raghavendrarai9367
@raghavendrarai9367 2 месяца назад
max height is 7 in dry-run example, please check.
@bizzie14
@bizzie14 Месяц назад
You have to reverse the input lists to get the right answer or fill the stacks starting from the last element of the list.
@rushabhkanhed5896
@rushabhkanhed5896 Год назад
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 Год назад
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 ?
@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
@malebeauty
@malebeauty 3 года назад
god :)
@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.
@mystudyspace5814
@mystudyspace5814 3 года назад
cool
@pritishpattnaik4674
@pritishpattnaik4674 3 года назад
bro i tried this code in c++ , but i got wrong testcases
@nikoo28
@nikoo28 3 года назад
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 3 года назад
@@nikoo28 bro i am getting the output as 0 instead of 5
@nikoo28
@nikoo28 3 года назад
Try to debug the code and print some values to analyse if your code is working as expected.
@collegematerial5348
@collegematerial5348 2 года назад
Please provide solution in c language anybody about this cumulative sum
@Zhouri
@Zhouri 11 месяцев назад
Below is a C++ sol :
@Zhouri
@Zhouri 11 месяцев назад
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(); }
@soumenmondal683
@soumenmondal683 3 года назад
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 3 года назад
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 3 года назад
@@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 3 года назад
@@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 3 года назад
@@nikoo28 Thank you sir
@vaibhavvarshney4523
@vaibhavvarshney4523 7 месяцев назад
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 7 месяцев назад
BTW i like the way you explain the problem statement and the solution.
Далее