|
| 1 | +# 950. Reveal Cards In Increasing Order |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Queue, Sorting, Simulation. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an integer array `deck`. There is a deck of cards where every card has a unique integer. The integer on the `ith` card is `deck[i]`. |
| 10 | + |
| 11 | +You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. |
| 12 | + |
| 13 | +You will do the following steps repeatedly until all cards are revealed: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +- Take the top card of the deck, reveal it, and take it out of the deck. |
| 18 | + |
| 19 | +- If there are still cards in the deck then put the next top card of the deck at the bottom of the deck. |
| 20 | + |
| 21 | +- If there are still unrevealed cards, go back to step 1. Otherwise, stop. |
| 22 | + |
| 23 | + |
| 24 | +Return **an ordering of the deck that would reveal the cards in increasing order**. |
| 25 | + |
| 26 | +**Note** that the first entry in the answer is considered to be the top of the deck. |
| 27 | + |
| 28 | + |
| 29 | +Example 1: |
| 30 | + |
| 31 | +``` |
| 32 | +Input: deck = [17,13,11,2,3,5,7] |
| 33 | +Output: [2,13,3,11,5,17,7] |
| 34 | +Explanation: |
| 35 | +We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it. |
| 36 | +After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck. |
| 37 | +We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13]. |
| 38 | +We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11]. |
| 39 | +We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. |
| 40 | +We reveal 7, and move 13 to the bottom. The deck is now [11,17,13]. |
| 41 | +We reveal 11, and move 17 to the bottom. The deck is now [13,17]. |
| 42 | +We reveal 13, and move 17 to the bottom. The deck is now [17]. |
| 43 | +We reveal 17. |
| 44 | +Since all the cards revealed are in increasing order, the answer is correct. |
| 45 | +``` |
| 46 | + |
| 47 | +Example 2: |
| 48 | + |
| 49 | +``` |
| 50 | +Input: deck = [1,1000] |
| 51 | +Output: [1,1000] |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +**Constraints:** |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +- `1 <= deck.length <= 1000` |
| 60 | + |
| 61 | +- `1 <= deck[i] <= 106` |
| 62 | + |
| 63 | +- All the values of `deck` are **unique**. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +## Solution |
| 68 | + |
| 69 | +```javascript |
| 70 | +/** |
| 71 | + * @param {number[]} deck |
| 72 | + * @return {number[]} |
| 73 | + */ |
| 74 | +var deckRevealedIncreasing = function(deck) { |
| 75 | + deck.sort((a, b) => a - b); |
| 76 | + var res = Array(deck.length); |
| 77 | + var i = 0; |
| 78 | + var j = 0; |
| 79 | + var skip = false; |
| 80 | + while (i < deck.length) { |
| 81 | + // find space for card |
| 82 | + if (res[j] === undefined) { |
| 83 | + // place card if it's not skip round |
| 84 | + if (!skip) { |
| 85 | + res[j] = deck[i]; |
| 86 | + i++; |
| 87 | + } |
| 88 | + // revert skip flag |
| 89 | + skip = !skip; |
| 90 | + } |
| 91 | + // try next place |
| 92 | + j = (j + 1) % deck.length; |
| 93 | + } |
| 94 | + return res; |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +**Explain:** |
| 99 | + |
| 100 | +nope. |
| 101 | + |
| 102 | +**Complexity:** |
| 103 | + |
| 104 | +* Time complexity : O(n * log(n)). |
| 105 | +* Space complexity : O(n). |
0 commit comments