Minimum Tokens Remaining
Let us consider an infinite sequence of stacks indexed from 0, and an exchange operation that removes two tokens from a stack and adds one token to the next stack.
For example, lets assume there are two tokens on stack 0 and three on stack 1. Two tokens from stack 0 may be exchanged for one new token on stack 1. After that operation, there are four tokens on stack 1 that may be exchanged for two new tokens on stack 2. Finally, a new token may be added to stack 3 by exchanging two tokens from stack 2. This gives us: stacks 0, 1 and 2 empty, and stack 3 with one token.
Given the heights of the first N stacks, find the minimum number of tokens that may remain after any number of exchange operations. You may assume that all of the tokens are identical. All uninitialized stacks are empty by default.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A of N integers, representing the heights of the first N stacks in the sequence, returns the minimum number of tokens which may remain on the stacks after any number of exchange operations.
1Example 1
A = [2, 3], the function should return 1, as explained above.