Tap here to switch tabs
Problem
Submissions

Problem: Bag of Tokens

med
30 min
Try to solve the Bag of Tokens problem.

Statement

You are given an integer array tokens and an integer power.

You begin with an initial score of 00. Your objective is to maximize your total score by playing tokens. Each token in tokens can be played at most once in any order, using one of the following moves:

  • Face up: If your current power is at least tokens[i], you can play token ii, losing tokens[i] power and gaining 11 score.

  • Face down: If your current score is at least 11, you can play token ii, gaining tokens[i] power and losing 11 score.

Return the maximum score you can achieve after playing any number of tokens.

Constraints:

  • 0≤0 \le tokens.length ≤1000\le 1000

  • 0≤0 \le tokens[i], power <104< 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Bag of Tokens

med
30 min
Try to solve the Bag of Tokens problem.

Statement

You are given an integer array tokens and an integer power.

You begin with an initial score of 00. Your objective is to maximize your total score by playing tokens. Each token in tokens can be played at most once in any order, using one of the following moves:

  • Face up: If your current power is at least tokens[i], you can play token ii, losing tokens[i] power and gaining 11 score.

  • Face down: If your current score is at least 11, you can play token ii, gaining tokens[i] power and losing 11 score.

Return the maximum score you can achieve after playing any number of tokens.

Constraints:

  • 0≤0 \le tokens.length ≤1000\le 1000

  • 0≤0 \le tokens[i], power <104< 10^4