left
and right
, to the start and end of the array respectively.score
and maxScore
to 0.left
<= right
, repeat the following steps:
a. While left
<= right
and power
>= tokens[left]
, play the left
token face up: increase score
by 1, decrease power
by tokens[left]
, and increment left
.
b. Update maxScore
to the maximum of maxScore
and score
.
c. If score
> 0 and left
<= right
, play the right
token face down: decrease score
by 1, increase power
by tokens[right]
, and decrement right
.maxScore
as the largest possible score.