Reveal Cards In Increasing Order (LeetCode 950): Solution and Explanation
Order a deck so that repeatedly revealing the top card and moving the next one to the bottom turns the cards up in increasing order.
Intuition
The reveal process never looks at card values. With n cards it always reveals the deck positions in the same fixed order.
So run the process on positions 0..n-1 instead of on cards. Whatever position comes off the front k-th is the slot that will be revealed k-th.
Since the cards must come out smallest first, the k-th revealed position gets the k-th smallest card.
Approach: Queue of positions
- Sort the cards in increasing order.
- Put positions 0..n-1 in a queue, front first.
- For each card in sorted order, pop the front position and write the card there.
- If positions remain, pop the next front position and push it to the back. Return the filled array.
Step-by-step example
deck = [17,13,11,2,3,5,7]
- Sorted cards: 2, 3, 5, 7, 11, 13, 17. Queue: 0 1 2 3 4 5 6.
- 2 goes to position 0; position 1 moves to the back. Queue: 2 3 4 5 6 1.
- 3 goes to position 2; 3 moves back. 5 goes to 4; 5 moves back. 7 goes to 6; 1 moves back.
- Queue is now 3 5 1. 11 goes to 3; 5 moves back. 13 goes to 1; 5 moves back. 17 goes to 5.
- Return [2,13,3,11,5,17,7].
Code: Python, Java, C++
1from collections import deque
2
3class Solution:
4 def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
5 n = len(deck)
6 q = deque(range(n))
7 res = [0] * n
8 for card in sorted(deck):
9 res[q.popleft()] = card
10 if q:
11 q.append(q.popleft())
12 return res1class Solution {
2 public int[] deckRevealedIncreasing(int[] deck) {
3 int n = deck.length;
4 Arrays.sort(deck);
5 Deque<Integer> q = new ArrayDeque<>();
6 for (int i = 0; i < n; i++) q.offer(i);
7 int[] res = new int[n];
8 for (int card : deck) {
9 res[q.poll()] = card;
10 if (!q.isEmpty()) q.offer(q.poll());
11 }
12 return res;
13 }
14}1class Solution {
2public:
3 vector<int> deckRevealedIncreasing(vector<int>& deck) {
4 int n = deck.size();
5 sort(deck.begin(), deck.end());
6 queue<int> q;
7 for (int i = 0; i < n; i++) q.push(i);
8 vector<int> res(n);
9 for (int card : deck) {
10 res[q.front()] = card; q.pop();
11 if (!q.empty()) { q.push(q.front()); q.pop(); }
12 }
13 return res;
14 }
15};Time and space complexity
O(n log n)
Sorting dominates. The queue simulation does O(1) work per card.
O(n)
The queue of positions and the answer array.
Other approaches
Reverse the process (time O(n log n), space O(n))
Take cards from largest to smallest and build the deck from the bottom up: move the bottom card to the top, then put the next card on top. Same complexity, a deque instead of a queue of indices.
Common mistakes
- Forgetting to skip the move-to-bottom step after the last card is placed, which pops from an empty queue.
- Simulating with the card values instead of positions, which leaves no way to know where each card belongs.
- Sorting with the default string comparison in JavaScript, which puts 1000 before 2.
Frequently asked questions
Why does the queue of positions give the reveal order?
The queue holds the current deck top to bottom, written as original positions. Popping the front is revealing the top card, and pushing the next front to the back is moving the top card to the bottom, exactly as the rules say.
Is the answer unique?
Yes. The order positions are revealed in is fixed by n, and the values must come out sorted, so every position has exactly one possible card.