Description

You have tokens with values and initial power. You can play a token face up (lose power, gain score) or down (gain power, lose score). Maximize score.

Examples

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

Can't play any token.

Input:tokens = [], power = 100
Output:0
Explanation:

Empty token array means no tokens to play, so maximum score is 0 regardless of initial power.

Input:tokens = [50, 75, 150, 250], power = 100
Output:2
Explanation:

Start with power=100. Play token 50 face-up (power=50, score=1), then token 75 face-up (power=-25, impossible). Instead, play 50 face-up (power=50, score=1), play 250 face-down (power=300, score=0), then play 75 face-up (power=225, score=1) and 150 face-up (power=75, score=2). Maximum achievable score is 2.

Constraints

  • 0 ≤ tokens.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!