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!
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(); } }
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.
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.
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..!!
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....
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.
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
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.
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?
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.
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++?
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
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.
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.
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?
@@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.
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.
if you are having problem with understanding the &&(and) condition, you can use the ||(or) condition like i have used. (st1TotalHeight > st2TotalHeight || st1TotalHeight > st3TotalHeight)
@@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
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
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--; } }
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)
@@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--; } }