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.
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.
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++; } } } }
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.
//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 ?
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
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.
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(); }
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. 👍
@@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
@@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.
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]