Skip to content

950. Reveal Cards In Increasing Order Medium

You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the i^{th} card is deck[i].

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

You will do the following steps repeatedly until all cards are revealed:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
  3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.

Return an ordering of the deck that would reveal the cards in increasing order.

Note that the first entry in the answer is considered to be the top of the deck.

Example 1:
Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.

Approach

Input: An integer array deck, representing a disordered deck of cards, where each number is distinct

Output: A reordered deck of cards, such that when revealed according to the problem's rules, the cards are displayed in ascending order

This problem belongs to the typical Basic Simulation Queue + Constructive Thinking category.

We use a queue index_queue to simulate the index sequence of the cards, and combine it with a sorted deck to reconstruct the arrangement of the original deck:

  1. First, sort the deck deck in ascending order, let's call it sorted_deck.
  2. Initialize a queue index_queue with contents [0, 1, 2, ..., n-1], representing which position each card should be placed in.
  3. Sequentially take the current minimum card from sorted_deck:
  4. Pop the front element i from index_queue, meaning this card will be placed in the i-th position of the result array.
  5. Then simulate the process of "putting the next card at the bottom": pop the new front element of index_queue and append it to the rear of the queue.
  6. Repeat the above operations until all cards are placed.
  7. The finally constructed result array is the original arrangement of the deck that guarantees an ascending reveal order.

Implementation

python
class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
        n = len(deck)
        deck.sort()  # Sort the deck first, representing the order we ultimately want to reveal

        # Create an index queue to simulate drawing cards from the top of the deck to the table
        index_queue = [i for i in range(n)]

        # Create a result array to place each card in its intended position
        result = [0] * n

        # Iterate over each card in the sorted deck (from smallest to largest)
        for card in deck:
            # Pop the position where the current card should be placed (simulates revealing top card)
            i = index_queue.pop(0)
            result[i] = card  # Place the current card in the corresponding position

            # Simulate step 2: put the next card to the bottom 
            # (inverse operation: move the next index to the end of the queue)
            # This essentially alternates placing minimum values into alternating indices
            if index_queue:
                index_queue.append(index_queue.pop(0))

        return result
javascript
/**
 * @param {number[]} deck
 * @return {number[]}
 */
const deckRevealedIncreasing = function(deck) {
    const indexQueue = Array(deck.length).fill(0).map((item, idx) => idx);
    const result = Array(deck.length).fill(0);
    deck.sort((a, b) => a - b);

    for (let card of deck) {
        const i = indexQueue.shift();
        result[i] = card;

        if (indexQueue.length) {
            indexQueue.push(indexQueue.shift());
        }
    }

    return result;
};

Complexity Analysis

  • Time Complexity: O(n log n) due to sorting. The queue operations take O(n).
  • Space Complexity: O(n) to store the result and the index queue.

950. Reveal Cards In Increasing Order (English)

950. 按递增顺序显示卡牌 (Chinese)