Start with power units of power and a score of zero. Each token may be used at most once, in either of two ways:
- Spend power equal to its value to gain one point, provided you have enough power.
- Spend one point to gain power equal to its value, provided your score is positive.
You may use tokens in any order and stop whenever you want. Return the greatest score you can reach.
Example 1:
Input: tokens = [50,75,100,200]
power = 75
Output: 2Explanation: Spend 50 power for one point, trade the 200 token for power, then spend 75 and 100 for two points. No third point can be reached with the available power and tokens.
Example 2:
Input: tokens = [0,0]
power = 0
Output: 2Explanation: Each zero-value token can be used to gain a point without spending power. Use both tokens to reach a score of 2.
Example 3:
Input: tokens = []
power = 100
Output: 0Explanation: There are no tokens to play, so the score stays at zero regardless of the starting power.
Constraints:
0 <= tokens.length <= 10000 <= tokens[i] <= 99990 <= power <= 9999