The title
Each card in the deck should have a unique integer pair. You can sort the cards in any order you want.
Initially, these cards are face down in the deck (i.e., no status is shown).
Now, repeat the following steps until all cards are displayed:
Draw a card from the top of the deck, display it, and then remove it from the deck. If there are still cards in the deck, the next card at the top of the deck is placed at the bottom of the deck. If there are still cards not shown, return to step 1. Otherwise, stop. Returns the deck order of cards that can be displayed in ascending order.
The first card in the answer is considered to be at the top of the pile.
Example: input: [17,13,11,2,3,5,7] output: [2,13,3,11,5,17,7] explanation: we get the deck order [17,13,11,2,3,5,7] (which is not important), and then reorder it. After reordering, the deck begins with [2,13,3,11,5,17,7], where 2 is at the top of the deck. We show 2, and then we move 13 to the bottom. Deck is now [3,11,5,17,7,13]. We display 3 and move 11 to the bottom. Deck is now [5,17,7,13,11]. We show 5, and then we move 17 to the bottom. Deck is now [7,13,11,17]. We display 7 and move 13 to the bottom. Deck is now [11,17,13]. We show 11, and then we move 17 to the bottom. The deck is now [13,17]. We show 13, and then we move 17 to the bottom. The deck is now [17]. We show 17. Since all the cards are displayed in ascending order, the answer is correct.Copy the code
Tip:
1 <= a. length <= 1000 1 <= A[I] <= 10^6 for all I! = j, A[I]! = A[j]
Their thinking
class Solution: def deckRevealedIncreasing(self, deck: List[int]) -> List[int]: Deck.sort () res = [deck.pop()] while len(deck)! = 0: res.append(res.pop(0)) res.append(deck.pop()) return res[::-1] if __name__ == '__main__': Deck = [17,13,11,2,3,5,7] ret = Solution(). DeckRevealedIncreasing (deck) print(ret)Copy the code