Skip to content

Bag of Tokens

MediumGreedy
Decode it first. Clues, constraints, pattern and plan before any code.

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.

tokens = [100], power = 50                 →  0
tokens = [200, 100], power = 150           →  1
tokens = [100, 200, 300, 400], power = 200 →  2

Constraints

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