Revealing Cards in Increasing Order Using Queue Simulation
Problem Description
Platform: LeetCode
Problem Number: 950
You are given an integer array deck where each card has a unique integer. You can arrange these cards in any order you want.
Initial, all cards are face down (hidden). Perform the following steps repeatedly until all cards are revealed:
- Reveal the top card from the deck and remove it.
- If the deck is not empty, move the next top card to the bottom of the deck.
- If there are still hidden cards, go back to step 1. Otherwise, stop.
Return an arrangement of the deck such that card are revealed in increasing order.
The first card in the answer is considered the top of the deck.
Example:
Input: [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
Starting with deck order [17,13,11,2,3,5,7], we need to rearrange it so that cards appear in increasing order when revealed.
After proper rearrangement to [2,13,3,11,5,17,7]:
- Reveal 2, move 13 to bottom → [3,11,5,17,7,13]
- Reveal 3, move 11 to bottom → [5,17,7,13,11]
- Reveal 5, move 17 to bottom → [7,13,11,17]
- Reveal 7, move 13 to bottom → [11,17,13]
- Reveal 11, move 17 to bottom → [13,17]
- Reveal 13, move 17 to bottom → [17]
- Reveal 17
All card are revealed in increasing order: 2, 3, 5, 7, 11, 13, 17.
Constraints:
2 <= deck.length <= 10001 <= deck[i] <= 10000- All values in
deckare distinct
Simulation Approach
The core insight is to first determine the reveal order for each card position, then place cards in ascending order according to that position.
We can simulate the reveal process using a deque:
- Record which position each original card gets revealed (using a hash map since all values are unique)
- Sort the cards to get ascending order
- Map each original card's reveal position to the corresponding sorted value
Java Solution:
import java.util.*;
class Solution {
public int[] deckRevealedIncreasing(int[] cards) {
int len = cards.length;
Map<Integer, Integer> revealOrder = new HashMap<>();
Deque<Integer> queue = new ArrayDeque<>();
// Initialize queue with original card order
for (int card : cards) {
queue.addLast(card);
}
// Record reveal position for each card
int currentOrder = 0;
while (!queue.isEmpty()) {
revealOrder.put(queue.pollFirst(), currentOrder++);
if (!queue.isEmpty()) {
queue.addLast(queue.pollFirst());
}
}
// Build result array
int[] result = new int[len];
int[] sorted = cards.clone();
Arrays.sort(sorted);
for (int i = 0; i < len; i++) {
result[i] = sorted[revealOrder.get(cards[i])];
}
return result;
}
}
C++ Solution:
#include <vector>
#include <map>
#include <deque>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& cards) {
int len = cards.size();
map<int, int> position;
deque<int> dq;
for (int card : cards) {
dq.push_back(card);
}
int order = 0;
while (!dq.empty()) {
position[dq.front()] = order++;
dq.pop_front();
if (!dq.empty()) {
dq.push_back(dq.front());
dq.pop_front();
}
}
vector<int> answer(len);
vector<int> sorted = cards;
sort(sorted.begin(), sorted.end());
for (int i = 0; i < len; i++) {
answer[i] = sorted[position[cards[i]]];
}
return answer;
}
};
Python3 Solution:
from collections import deque
from typing import List
class Solution:
def deckRevealedIncreasing(self, cards: List[int]) -> List[int]:
size = len(cards)
position = {}
dq = deque(cards)
order = 0
while dq:
position[dq.popleft()] = order
order += 1
if dq:
dq.append(dq.popleft())
sorted_cards = sorted(cards)
return [sorted_cards[position[cards[i]]] for i in range(size)]
TypeScript Solution:
function deckRevealedIncreasing(cards: number[]): number[] {
const size = cards.length;
const position: Record<number, number> = {};
const queue: number[] = [...cards];
let order = 0;
while (queue.length > 0) {
position[queue.shift()!] = order++;
if (queue.length > 0) {
queue.push(queue.shift()!);
}
}
const sorted = [...cards].sort((a, b) => a - b);
const result = new Array(size);
for (let i = 0; i < size; i++) {
result[i] = sorted[position[cards[i]]];
}
return result;
}
Complexity Analysis
-
Time Complexity: O(n log n)
- Queue simulation: O(n) — each element enters and exits the queue once
- Sorting: O(n log n)
- Building answer: O(n)
- Total: O(n log n)
-
Space Complexity: O(n)
- Hash map stores n entries
- Queue/deque stores n elements
- Additional arrays for sorting and result