Skip to main content
Queue

Reveal Cards In Increasing Order - LeetCode Daily Challenge

Aakash Verma

Problem Statement

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].

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.

Example 2

Input: deck = [1,1000]
Output: [1,1000]

Try here before watching the video.

Video Solution

Java Code

class Solution {
    public int[] deckRevealedIncreasing(int[] deck) {

        Arrays.sort(deck);

        Queue<Integer> queue = new LinkedList<>();
        for(int i = 0; i < deck.length; i++) {
            queue.add(i);
        }

        int[] answer = new int[deck.length];
        int ptr = 0;

        while(!queue.isEmpty()) {
            int index = queue.poll();
            if(!queue.isEmpty()) {
                queue.offer(queue.poll());
            }
            answer[index] = deck[ptr];
            ptr += 1;
        }

        return answer;
    }
}

C++ Code

class Solution {
public:
    vector<int> deckRevealedIncreasing(vector<int>& deck) {
        sort(deck.begin(), deck.end());
        
        queue<int> queue;
        for (int i = 0; i < deck.size(); ++i) {
            queue.push(i);
        }
        
        vector<int> answer(deck.size());
        int ptr = 0;
        
        while (!queue.empty()) {
            int index = queue.front();
            queue.pop();
            if (!queue.empty()) {
                queue.push(queue.front());
                queue.pop();
            }
            answer[index] = deck[ptr++];
        }
        
        return answer;
    }
};

Python Code

class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
        deck.sort()
        
        queue = list(range(len(deck)))
        answer = [0] * len(deck)
        ptr = 0
        
        while queue:
            index = queue.pop(0)
            answer[index] = deck[ptr]
            ptr += 1
            if queue:
                queue.append(queue.pop(0))
                
        return answer

Javascript Code

var deckRevealedIncreasing = function(deck) {
    deck.sort((a, b) => a - b);
    
    let queue = [];
    for (let i = 0; i < deck.length; i++) {
        queue.push(i);
    }
    
    let answer = new Array(deck.length).fill(0);
    let ptr = 0;
    
    while (queue.length > 0) {
        let index = queue.shift();
        answer[index] = deck[ptr++];
        if (queue.length > 0) {
            queue.push(queue.shift());
        }
    }
    
    return answer;
};

Go Code

func deckRevealedIncreasing(deck []int) []int {
    sort.Ints(deck)
    
    queue := make([]int, len(deck))
    for i := range deck {
        queue[i] = i
    }
    
    answer := make([]int, len(deck))
    ptr := 0
    
    for len(queue) > 0 {
        index := queue[0]
        queue = queue[1:]
        answer[index] = deck[ptr]
        ptr++
        if len(queue) > 0 {
            queue = append(queue[1:], queue[0])
        }
    }
    
    return answer
}

Complexity Analysis

Time Complexity: O(N logN), because we sorted the input array and iterated over the queue until it became empty.
Space Complexity: O(N), because we took queue and an answer array.