You are given a list of token values (tokens) and an initial amount of energy called power.
You also start with score = 0.
Each token can be used only one time, and you have two possible choices when using a token:
| Action | Condition | Result |
|---|---|---|
| Use Face-Up | If power >= tokens[i] | power -= tokens[i], score += 1 |
| Use Face-Down | If score >= 1 | power += tokens[i], score -= 1 |
Your goal is to maximize the score after using zero or more tokens.
Return the highest score possible.
Input: tokens = [60], power = 30
Output: 0
Explanation:
Not enough power to use the token face-up, and score is 0 so cannot play face-down.
Input: tokens = [90, 40], power = 70
Output: 1
Explanation:
Play token with value 40 (face-up): power = 70 - 40 = 30, score = 1 Cannot use token 90 since power is not enough.
Input: tokens = [50, 120, 200, 80], power = 130
Output: 2
Explanation:
Use 50 face-up → power=80, score=1 Use 200 face-down → power=280, score=0 Use 120 face-up → power=160, score=1 Use 80 face-up → power=80, score=2
Accepted:
Submission: