Тёмный

Equal Stacks HackerRank Solution | Data Structures | Stacks 

JAVAAID - Coding Interview Preparation
Подписаться 37 тыс.
Просмотров 22 тыс.
50% 1

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

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 79   
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
JAVAAID community: RU-vid recently changed the way they monetize my content. My channel now needs 1,000 subscribers so it would be awesome if you could show your support by both watching my videos and subscribing to my channel. please hit the like button too, if you find it useful. if you haven’t already done so. Monetizing my videos allows me to invest back into the channel with new equipment so a small gesture from you goes a huge way for me. Many thanks for your support!
@anupsubba7184
@anupsubba7184 3 года назад
Really great solution.. but I won't maintain 3 variables for height. Hence using peek() for every if-else instead of storing the values in those variables. But I don't know why my code is not working. Here are both the code snippet. please look into it and suggest why my code is not working. Works totally fine according to your way. public static int equalStacksWithHeightVariable(List h1, List h2, List h3) { final Stack st1 = new Stack(); final Stack st2 = new Stack(); final Stack st3 = new Stack(); int st1totalheight = 0, st2totalheight = 0, st3totalheight = 0; for (int i = h1.size() - 1; i >= 0; i--) { st1totalheight += h1.get(i); st1.push(st1totalheight); } for (int i = h2.size() - 1; i >= 0; i--) { st2totalheight += h2.get(i); st2.push(st2totalheight); } for (int i = h3.size() - 1; i >= 0; i--) { st3totalheight += h3.get(i); st3.push(st3totalheight); } while (true) { if (st1.isEmpty() || st2.isEmpty() || st3.isEmpty()) return 0; st1totalheight = st1.peek(); st2totalheight = st2.peek(); st3totalheight = st3.peek(); if (st1totalheight == st2totalheight && st2totalheight == st3totalheight) return st1totalheight; else if (st1totalheight >= st2totalheight && st1totalheight >= st3totalheight) st1.pop(); else if (st2totalheight >= st1totalheight && st2totalheight >= st3totalheight) st2.pop(); else if (st3totalheight >= st2totalheight && st3totalheight >= st1totalheight) st3.pop(); } } Not Working. public static int equalStacksWithoutHeightVariable(List h1, List h2, List h3) { final Stack s1 = new Stack(); final Stack s2 = new Stack(); final Stack s3 = new Stack(); int height = 0; for(int i=h1.size()-1; i>=0; i--) { height += h1.get(i); s1.push(height); } height=0; for(int i=h2.size()-1; i>=0; i--) { height += h2.get(i); s2.push(height); } height=0; for(int i=h3.size()-1; i>=0; i--) { height += h3.get(i); s3.push(height); } while(true) { if(s1.isEmpty() || s2.isEmpty() || s3.isEmpty()) return 0; if(s1.peek() == s2.peek() && s2.peek() == s3.peek()) return s1.peek(); else if(s1.peek() >= s2.peek() && s1.peek() >= s3.peek()) s1.pop(); else if(s2.peek() >= s1.peek() && s2.peek() >= s3.peek()) s2.pop(); else if(s3.peek() >= s2.peek() && s3.peek() >= s1.peek()) s3.pop(); } }
@minhhuyvo9380
@minhhuyvo9380 4 года назад
Thanks for showing the solution i was not knowing how to do this task, but this help me understand the logic behind it !
@JavaAidTutorials
@JavaAidTutorials 4 года назад
welcome :)
@shrutipawar3351
@shrutipawar3351 5 лет назад
Your tutorials provide the best explanations ever...! Please make more such videos Sir..!
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks a lot, Shruti for your wonderful comment and this is the best comment which I have received ever. :) Keep watching, keep commenting. It really motivates me to do more and better for you all. please hit the like button too, if you find it useful.
@josejvazquez-perez9673
@josejvazquez-perez9673 4 года назад
I have problems trying to find an algorithmic solution to problems... The way you saw the problem, I just couldn't see it. Of course after seeing the problem completely solved I'm like: "oh wow, I understand perfectly how he did it!" but I just can't solve them by myself.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
the approach towards solving the problem is a skill and learning a skill will take time. so do not worry if you are not able to get the correct algorithm in one go, even I was also like you at some point but slowly -slowly you will see the improvement and able to use the same technique on those question which follows the same pattern. keep learning and keep coding..!!
@zenshyam
@zenshyam 4 года назад
At first while lokking at the question the logic does not click in my mind and not able to think upto the mark...but after watching the videos I am able to understand the logic and solution....So is this the wrong way to practise DSA or something I can do to improve myself.....Any suggestion will be appreciable....Thanking you in advance....BTW your videos are really amazing and easy to understand....Thanks a lot for this effort....
@JavaAidTutorials
@JavaAidTutorials 4 года назад
you can follow the video description to know more about good learning resources for DSA and thanks for your feedback..🙂
@codingfreek5737
@codingfreek5737 5 лет назад
Good explanation bhai .. your all tutorials are very useful and easy to understand..Thanks for sharing..
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
thanks a lot brother..Keep coding..and thanks for watching..!!😊
@sankeerthmeda6088
@sankeerthmeda6088 3 года назад
such a clean code impressed with your algorithm....!
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Happy to hear that!
@SwarnaliMollickA
@SwarnaliMollickA 4 года назад
Thank You Sir.I have written this code in Java and it works.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Most welcome.😊
@nishchalbhardwaj4835
@nishchalbhardwaj4835 3 года назад
on line 59, shouldn't u have s2height >= s1height Thanks for the explanation tho.
@vikassanwal1818
@vikassanwal1818 3 года назад
Because of weak test cases, this mistake didn't make any impact.
@srividya6263
@srividya6263 3 года назад
Great solution
@JavaAidTutorials
@JavaAidTutorials 3 года назад
thanks :)
@pankajrathi
@pankajrathi 4 года назад
At 13:22 it has a small bug in line number 59. You have put the same condition in both of them. you have to compare s2 > s3 and s2 > s1.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thanks Pankaj for highlighting this, may be i could not catch this because all TC was passing but now i have done the changes in source code accordingly.
@AnkitKumarCoder
@AnkitKumarCoder 4 года назад
Greatly explained!!!
@surajtopal9940
@surajtopal9940 4 года назад
Crystal clear 🤗🤗👌👌
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thanks :)
@rakeshk4846
@rakeshk4846 5 лет назад
Hello sir, thanks for making a video on equal stacks problem. Sir I want to know how to write optimized code for a problem. Can u guide me. Waiting for ur reply
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hi Rakesh, Solving a problem will not take much time but optimizing a solution is an art which takes time. I will suggest you to keep on solving the different-2 problem to become an expert in optimization. only practice is a way to become better. You can watch my tutorials which will teach you some of the other tips and tricks which you can use any other problems.
@rdrahuldhiman19
@rdrahuldhiman19 4 года назад
Sir thanks for not just sharing the info but also teaching us how to be a better programmer..and can u advice on what it really takes to be good at problem solving?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Keep practicing, you will eventually become better. But before doing practice first learn the concept. You can go through video description to know more about good resources which can help you to build your foundations.
@rdrahuldhiman19
@rdrahuldhiman19 4 года назад
@@JavaAidTutorials thank you sir(...bro)
@eklavyaprasad5009
@eklavyaprasad5009 3 года назад
Thank you sir. I too have the same approach as you but I got TLE so.. I came here. And amazed by your solution as the logic was same but I can't implement it. But after your explanation video. It is feeded in my memory. Ps. Can you tell me is java is good enough for competitive programming? Or should I switch to C++?
@JavaAidTutorials
@JavaAidTutorials 3 года назад
there is no doubt C++ is faster than java but at the same time, java has good amount of inbuilt library which can make your code shorter and easier and you can finish your code before your competitor
@rafatashraf3669
@rafatashraf3669 5 лет назад
Thanks . Keep up that good work .
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
most welcome..:)
@baidya87
@baidya87 4 года назад
nice explanation. thanks !
@JavaAidTutorials
@JavaAidTutorials 4 года назад
You are welcome!
@prashantverma3347
@prashantverma3347 4 года назад
Well explained . Thank you.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
welcome. 🙂
@mohantraju
@mohantraju 2 года назад
Thanks for the video..but submitting this in hackerrank, still many test cases fails. can you pls check.
@amandeepsingh1730
@amandeepsingh1730 4 года назад
pls solve using stack in c++
@JavaAidTutorials
@JavaAidTutorials 4 года назад
sorry, I don't know C++ but I always try to explain the generic algorithm which can be implemented in any programming language. I am planning to provide at least text solution in most of the common language in near future. but all depends on your support , please support us by sharing our channel.
@RohanKKayan
@RohanKKayan 3 года назад
Can we improve the conditional check to something like this if(A>=B && A>=C) popA(); else if(B>=C) popB(); else popC() ??
@ravipanchal2259
@ravipanchal2259 5 лет назад
Great explanation, Thanks for sharing.
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks for watching..!! 😊
@ruchikachirania7007
@ruchikachirania7007 4 года назад
Can you also explain waiter problem ??
@hrittikgarain4674
@hrittikgarain4674 3 года назад
superb
@JavaAidTutorials
@JavaAidTutorials 3 года назад
Thanks 🤗
@top10thingwhichyoushouldkn37
@top10thingwhichyoushouldkn37 4 года назад
why we start loop from backwad ? why we want first element to come top of stack ? can we use normal loop from start to end
@JavaAidTutorials
@JavaAidTutorials 4 года назад
if you see the sample input and follow the given diagram in problem statement you will find first element in the array represent top of the stack that is the reason why we have looped the item backward so that first item will come to stack. because stack maintains LIFO order.
@subratasen12
@subratasen12 5 лет назад
Hello, I have understood the answer but still i think it need some modification. In the 59th line of the solution why using same condition twice also seems like that line condition is not right as it's not checking the value of second sum with the first value of first sum?
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
line no 59 is st2.pop(); // are you talking about popping statement??
@subratasen12
@subratasen12 5 лет назад
@@JavaAidTutorials Please check the video properly.. Line 59. else if(st2TotalHeight >= st3TotalHeight && st2TotalHeight >= st3TotalHeight )
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
@@subratasen12Sen yaa got it., actually I was reffering my source code which I upload on git, In that, it was #59 but did not realalize you are talking about video solution..:) now I got your point. you are partially correct. You do not need to check that condition explicitly that will be automatically covered in side else block but condition what ever I am checking that is correct only.
@rishabhdubey4842
@rishabhdubey4842 5 лет назад
superb 😃
@JavaAidTutorials
@JavaAidTutorials 4 года назад
thank you..:)
@qR7pK9sJ2t
@qR7pK9sJ2t 4 года назад
sir why have u used >= sign from in line 57 59 61.. u could have used > sign.. plzz reply
@JavaAidTutorials
@JavaAidTutorials 4 года назад
what if, any two of the stack height is equal something like this- int st1TotalHeight = 20, st2TotalHeight = 20, st3TotalHeight = 10; without equal :- if (st1TotalHeight > st2TotalHeight && st1TotalHeight > st3TotalHeight) // line 56, it will return false and will not pop the element. with equal :- if (st1TotalHeight >= st2TotalHeight && st1TotalHeight >= st3TotalHeight) // line 56, it will return true and will pop the element st1.pop(); I hope it answer your question.
@axbax93
@axbax93 4 года назад
if you are having problem with understanding the &&(and) condition, you can use the ||(or) condition like i have used. (st1TotalHeight > st2TotalHeight || st1TotalHeight > st3TotalHeight)
@koushiks9292
@koushiks9292 4 года назад
@@JavaAidTutorials this was my doubt too .....but you cleared it undoubtedly ...thankyou so much sir...when i dont understand anything ,i just check ur playlist ,it clears my mind.....i code in cpp....still no problem......keep helping us sir
@rushabhkanhed5896
@rushabhkanhed5896 Год назад
@@JavaAidTutorials Hello sir, if 2 stacks are equal like st1 and st2 then we should pop element from both stack but you pop it from only one.
@sumitraj8143
@sumitraj8143 4 года назад
sir,i m not able to pass all the test cases package hackerrank; import java.util.Scanner; import java.util.Stack; public class EqualStacks { static int equalStacks(int[] h1, int[] h2, int[] h3) { int temp1=0; int temp2=0; int temp3=0; Stack s1=new Stack(); Stack s2=new Stack(); Stack s3=new Stack(); for(int i=0;i=s1.peek()&&s2.peek()>=s3.peek()) { s2.pop(); } else if(s3.peek()>=s1.peek()&&s3.peek()>=s2.peek()) { s3.pop(); } } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); int y=sc.nextInt(); int z=sc.nextInt(); int h1[]=new int[x]; for(int i=0;i
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I could see while pushing into s3 stack you are using h2 length by mistake, for(int i=0;i
@adityaraul2175
@adityaraul2175 3 года назад
Sir please could you tell me what is wrong with my code, import java.util.*; class Result { public static int equalStacks(int[] h1, int[] h2, int[] h3) { Stack stack1 = new Stack(); Stack stack2 = new Stack(); Stack stack3 = new Stack(); int height1 = 0, height2 = 0, height3 = 0; addToStack(h1, stack1, height1); addToStack(h2, stack2, height2); addToStack(h3, stack3, height3); while(true) { if(stack1.isEmpty() || stack2.isEmpty() || stack3.isEmpty()) return 0; height1 = stack1.peek(); height2 = stack2.peek(); height3 = stack3.peek(); if(height1 == height2 && height2 == height3) return height1; if(height1 >= height2 && height1 >= height3) { stack1.pop(); } else if(height2 >= height1 && height2 >= height3) { stack2.pop(); } else if(height3 == height2 && height3 == height1) { stack3.pop(); } } } public static void addToStack(int[] h, Stack stack, int height) { for(int i = h.length -1; i >= 0; i++) { height += h[i]; stack.push(height); } } } class Solution { public static void main(String [] args) { Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); int n2 = sc.nextInt(); int n3 = sc.nextInt(); int h1[] = new int[n1]; for (int i = 0; i < n1; i++) { h1[i] = sc.nextInt(); } int h2[] = new int[n2]; for (int i = 0; i < n2; i++) { h2[i] = sc.nextInt(); } int h3[] = new int[n3]; for (int i = 0; i < n3; i++) { h3[i] = sc.nextInt(); } System.out.println(Result.equalStacks(h1, h2, h3)); sc.close(); } }
@amanrai8010
@amanrai8010 5 лет назад
Sir I have codded sam in C but not able to get solution #include #include int main() { long int n1,n2,n3; scanf("%ld",&n1); scanf("%ld",&n2); scanf("%ld",&n3); long int s1[n1],s2[n2],s3[n3],top1=-1,top2=-1,top3=-1,i,j,k,e,sum=0,h1,h2,h3; for(i=0;i=h1 && h2>=h3) top2--; else if(h3>=h1 && h3>=h2) top3--; } }
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
I can see two problems in your code- 1) while storing the data into the array and retrieving the data. here you are using an array as an array but you have to use it as stack it means use LIFO order for operations. 2) if(h1>=h2 && h2>=h3) is not correct make it if(h1>=h2 && h1>=h3)
@amanrai8010
@amanrai8010 5 лет назад
@@JavaAidTutorials Sir I have modified this but still not got the answer #include #include int main() { long int n1,n2,n3; scanf("%ld",&n1); scanf("%ld",&n2); scanf("%ld",&n3); long int s1[n1],s2[n2],s3[n3],top1=-1,top2=-1,top3=-1,i,j,k,e,sum=0,h1,h2,h3; while(top1=h1 && h2>=h3) top2--; else if(h3>=h1 && h3>=h2) top3--; } }
@shivanisoni6551
@shivanisoni6551 Год назад
@@amanrai8010 can you please tell the final solution?
@amanrai8010
@amanrai8010 Год назад
@@shivanisoni6551 Hi Shivani, It has been years since I wrote this code. I don't even remember C now. I code in Java now.