|
| 1 | +# 950. Reveal Cards In Increasing Order |
| 2 | + |
| 3 | +**<font color=red>难度: Medium</font>** |
| 4 | + |
| 5 | +## 刷题内容 |
| 6 | + |
| 7 | +> 原题连接 |
| 8 | +
|
| 9 | +* https://leetcode.com/problems/reveal-cards-in-increasing-order/ |
| 10 | + |
| 11 | +> 内容描述 |
| 12 | +
|
| 13 | +``` |
| 14 | +In a deck of cards, every card has a unique integer. You can order the deck in any order you want. |
| 15 | +
|
| 16 | +Initially, all the cards start face down (unrevealed) in one deck. |
| 17 | +
|
| 18 | +Now, you do the following steps repeatedly, until all cards are revealed: |
| 19 | +
|
| 20 | +Take the top card of the deck, reveal it, and take it out of the deck. |
| 21 | +If there are still cards in the deck, put the next top card of the deck at the bottom of the deck. |
| 22 | +If there are still unrevealed cards, go back to step 1. Otherwise, stop. |
| 23 | +Return an ordering of the deck that would reveal the cards in increasing order. |
| 24 | +
|
| 25 | +The first entry in the answer is considered to be the top of the deck. |
| 26 | +
|
| 27 | + |
| 28 | +
|
| 29 | +Example 1: |
| 30 | +
|
| 31 | +Input: [17,13,11,2,3,5,7] |
| 32 | +Output: [2,13,3,11,5,17,7] |
| 33 | +Explanation: |
| 34 | +We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. |
| 35 | +After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck. |
| 36 | +We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13]. |
| 37 | +We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11]. |
| 38 | +We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. |
| 39 | +We reveal 7, and move 13 to the bottom. The deck is now [11,17,13]. |
| 40 | +We reveal 11, and move 17 to the bottom. The deck is now [13,17]. |
| 41 | +We reveal 13, and move 17 to the bottom. The deck is now [17]. |
| 42 | +We reveal 17. |
| 43 | +Since all the cards revealed are in increasing order, the answer is correct. |
| 44 | + |
| 45 | +
|
| 46 | +Note: |
| 47 | +
|
| 48 | +1 <= A.length <= 1000 |
| 49 | +1 <= A[i] <= 10^6 |
| 50 | +A[i] != A[j] for all i != j |
| 51 | +``` |
| 52 | + |
| 53 | +## 解题方案 |
| 54 | + |
| 55 | +> 思路 1 |
| 56 | +******- 时间复杂度: O(NlgN)******- 空间复杂度: O(1)****** |
| 57 | + |
| 58 | +逆向思维,倒着回去就行 |
| 59 | + |
| 60 | +```python |
| 61 | +class Solution: |
| 62 | + def deckRevealedIncreasing(self, deck): |
| 63 | + """ |
| 64 | + :type deck: List[int] |
| 65 | + :rtype: List[int] |
| 66 | + """ |
| 67 | + deck.sort() |
| 68 | + res = collections.deque([deck[-1]]) |
| 69 | + for i in deck[:-1][::-1]: |
| 70 | + tmp = res.pop() |
| 71 | + res.appendleft(tmp) |
| 72 | + res.appendleft(i) |
| 73 | + return [i for i in res] |
| 74 | +``` |
0 commit comments