energy energy, 0 points and a deck of cards: cards[i] is the cost of card i. Each card can be played at most once, in one of two ways:
- **buy**: if your energy is at least cards[i], spend cards[i] energy and gain 1 point;
- **cash in**: if you have at least 1 point, give up 1 point and gain cards[i] energy.
You may play the cards in any order and leave some of them unplayed. Return the largest number of points you can hold at any moment of the game.cards=[30, 80, 10, 50], energy=40 → 2
Spend on 10 (energy 30, score 1), spend on 30 (energy 0, score 2), cash in 80 (energy 80, score 1), spend on 50 (energy 30, score 2). No line of play scores more than 2.
cards=[25], energy=20 → 0
There is not enough energy, and a card cannot be cashed in without a point, so the answer is 0.
cards=[5, 5, 5], energy=15 → 3
cards=[30, 80, 10, 50], energy=40
2
Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.