Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Bag of Tokens

XPChallenge Points: 20
levelLevel: Medium

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:

ActionConditionResult
Use Face-UpIf power >= tokens[i]power -= tokens[i], score += 1
Use Face-DownIf score >= 1power += tokens[i], score -= 1

Your goal is to maximize the score after using zero or more tokens.
Return the highest score possible.

Example 1:

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.

Example 2:

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.

Example 3:

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

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result