Skip to content
Decoder

Bag of Tokens

Medium

Problem

You start with power and a score of 0. Each token in tokens can be played at most once, in any order, in one of two ways:

- Face up: if your power is at least tokens[i], lose tokens[i] power and gain 1 score. - Face down: if your score is at least 1, gain tokens[i] power and lose 1 score.

You do not have to play every token. Return the largest score you can reach at any point.

Example 1

Input
tokens = [100], power = 50
Output
0

Example 2

Input
tokens = [200, 100], power = 150
Output
1

Example 3

Input
tokens = [100, 200, 300, 400], power = 200
Output
2

Constraints

  • 0 <= tokens.length <= 1000
  • 0 <= tokens[i], power < 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.