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.
Sure. This code will work for any number of stacks not just 3 as in the problem.
Read the first line and split it on the spaces to see how many stacks there are.
Read each line of numbers, convert to a queue and store them all in an array.
Create another array that contains the heights of all the stacks. This is so we don't have to repeatedly sum the blocks in the stacks.
While the stacks don't all have the same height compute the maximum height, find the index of the elem in heights that has that value then pop off the first elem in the corresponding stack and subtract from the height stored in heights array.
Print out the height of the first (or any other) stack.
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Sure. This code will work for any number of stacks not just 3 as in the problem.
Read the first line and split it on the spaces to see how many stacks there are.
Read each line of numbers, convert to a queue and store them all in an array.
Create another array that contains the heights of all the stacks. This is so we don't have to repeatedly sum the blocks in the stacks.
While the stacks don't all have the same height compute the maximum height, find the index of the elem in heights that has that value then pop off the first elem in the corresponding stack and subtract from the height stored in heights array.
Print out the height of the first (or any other) stack.