from collections import deque
class Solution:
def deckRevealedIncreasing(self, deck):
deck.sort()
n = len(deck)
index_queue = deque(range(n))
result = [0] * n
for card in deck:
idx = index_queue.popleft()
result[idx] = card
if index_queue:
index_queue.append(index_queue.popleft())
return result
#include <vector>
#include <deque>
#include <algorithm>
class Solution {
public:
std::vector<int> deckRevealedIncreasing(std::vector<int>& deck) {
std::sort(deck.begin(), deck.end());
int n = deck.size();
std::deque<int> index_queue;
for (int i = 0; i < n; ++i)
index_queue.push_back(i);
std::vector<int> result(n);
for (int card : deck) {
int idx = index_queue.front();
index_queue.pop_front();
result[idx] = card;
if (!index_queue.empty()) {
index_queue.push_back(index_queue.front());
index_queue.pop_front();
}
}
return result;
}
};
import java.util.*;
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
Arrays.sort(deck);
int n = deck.length;
Deque<Integer> indexQueue = new ArrayDeque<>();
for (int i = 0; i < n; ++i) indexQueue.add(i);
int[] result = new int[n];
for (int card : deck) {
int idx = indexQueue.pollFirst();
result[idx] = card;
if (!indexQueue.isEmpty()) {
indexQueue.add(indexQueue.pollFirst());
}
}
return result;
}
}
var deckRevealedIncreasing = function(deck) {
deck.sort((a, b) => a - b);
const n = deck.length;
const indexQueue = [];
for (let i = 0; i < n; ++i) indexQueue.push(i);
const result = new Array(n);
for (let card of deck) {
let idx = indexQueue.shift();
result[idx] = card;
if (indexQueue.length) {
indexQueue.push(indexQueue.shift());
}
}
return result;
};
You are given an array deck
representing a deck of unique integer cards. You are to arrange the deck so that when you reveal cards in the following process, the revealed cards are in increasing order:
You must return the initial ordering of deck
that will result in an increasing sequence of revealed cards. Each card must be used exactly once, and there is only one valid solution for each input deck.
At first glance, the problem seems to ask for simulating the reveal process and finding an arrangement that works. A brute-force approach would try all permutations, simulate the reveal, and check if it results in an increasing sequence. However, this is computationally infeasible for larger decks due to factorial growth.
By analyzing the process, we realize the challenge is to "reverse engineer" the reveal operation: given a sorted list of cards, how should we place them so that the reveal order is increasing? This leads us to consider working backwards, simulating the process in reverse, or using a data structure to track indices as we assign cards to their correct positions.
deck
in ascending order.
This approach uses a queue to mimic the reveal process in reverse, efficiently determining where each card should be placed.
Consider deck = [17, 13, 11, 2, 3, 5, 7]
.
[2, 3, 5, 7, 11, 13, 17]
.[_, _, _, _, _, _, _]
.[0, 1, 2, 3, 4, 5, 6]
.[2, _, _, _, _, _, _]
, queue: [1,2,3,4,5,6][2, _, 3, _, _, _, _]
, queue: [3,4,5,6,1][2, _, 3, _, 5, _, _]
, queue: [5,6,1,3][2, _, 3, _, 5, _, 7]
, queue: [1,3,5][2, _, 3, 11, 5, _, 7]
, queue: [5,1][2, 13, 3, 11, 5, _, 7]
, queue: [5][2, 13, 3, 11, 5, 17, 7]
[2, 13, 3, 11, 5, 17, 7]
. Revealing cards using the process will give [2, 3, 5, 7, 11, 13, 17]
.
O(n! * n)
, which is infeasible for large n
.
O(n \log n)
n
cards: each operation is O(1)
, so total O(n)
O(n \log n)
O(n)
for the result array and queueThis problem requires you to reconstruct the initial deck order so that a specific card-revealing process results in an increasing sequence. The key insight is to simulate the process in reverse using a queue of indices, efficiently assigning each sorted card to its correct place. This leads to an elegant and efficient solution that avoids brute-force enumeration and leverages simple data structures.