[ INFO ]category: Coding difficulty: medium freq: Must first seen: 2026-03-13
[MEDIUM][CODING][MUST]
$catproblem.md
Practice/Oracle/Leetcode 948. Bag of Tokens
Leetcode 948. Bag of Tokens
CodingMust
Problem
You are given an array of integers called tokens where each token has a point value, and you start with an initial amount of power. You can play tokens in two different ways:
Face-up: If your current power is at least equal to a token's value, you can play that token face-up. This costs you power equal to the token's value and grants you 1 point of score.
Face-down: If your current score is at least 1, you can play any token face-down. This costs you 1 point of score and grants you power equal to the token's value.
Each token can only be played once. Your goal is to maximize your final score after making any number of moves (including zero moves).
Requirements
Return the maximum possible score you can achieve
Each token can be used at most once
You may play tokens in any order you choose
You can switch between face-up and face-down plays as needed
If you cannot make any beneficial moves, you should stop
Constraints
0 ≤ tokens.length ≤ 1000
0 ≤ tokens[i] ≤ 10000
0 ≤ power ≤ 10000
Time complexity should be O(n log n) or better
Space complexity should be O(1) excluding the space needed for sorting
Examples
Example 1:
Input: tokens = [100], power = 50 Output: 0 Explanation: We don't have enough power to play the token face-up, and we have no score to play it face-down. The maximum score is 0.