cards[0], cards[1], … While the deck is not empty, one move is made:
- if the top card is the smallest of the cards still in the deck, it is removed;
- otherwise it is moved to the very bottom of the deck.
How many moves does it take to empty the deck?
Simulating directly can take on the order of n² moves; try to count the answer faster.cards=[5, 2, 7] → 5
5 is not the smallest, so it goes to the bottom: [2, 7, 5]. Remove 2: [7, 5]. 7 goes to the bottom: [5, 7]. Remove 5, then 7. Five moves in total.
cards=[3, 1, 2] → 4
One move sends 3 to the bottom, then 1, 2 and 3 come off one by one: 1 + 3 = 4.
cards=[-4, 0, 9] → 3
The deck is already in increasing order, so every top card is removed right away.
cards) ≤ 10⁵cards[i] ≤ 10⁹cards are distinctcards=[5, 2, 7]
5
Run checks the first 3 tests in your browser. Submit checks all 214 tests on the server, hidden ones included.