We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
There is a simple rule to solve this problem: always remove the block from stack i such that the height of i, minus the top block, is maximized.
This always works since the resulting stack is as tall as possible, such that it may admit a solution.
Be careful not to make your algorithm O(n^2), which can happen if you compute the height at each iteration by walking the entire (remainder of) the height arrays.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
This seems a bit overcomplicated.
There is a simple rule to solve this problem: always remove the block from stack i such that the height of i, minus the top block, is maximized.
This always works since the resulting stack is as tall as possible, such that it may admit a solution.
Be careful not to make your algorithm O(n^2), which can happen if you compute the height at each iteration by walking the entire (remainder of) the height arrays.