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:
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]
Constraints:
1 <= deck.length <= 1000
1 <= deck[i] <= 106
deck
are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for revealing cards involves trying every possible order of the cards. We want to find the order that satisfies the card revealing rules. This means we will generate each possible arrangement and test if it works.
Here's how the algorithm would work step-by-step:
import itertools
def reveal_cards_in_increasing_order_brute_force(deck):
number_of_cards = len(deck)
possible_arrangements = list(itertools.permutations(deck))
valid_arrangements = []
for arrangement in possible_arrangements:
arrangement_list = list(arrangement)
revealed = [False] * number_of_cards
current_index = 0
revealed_cards = []
successful = True
#Iterate to reveal the cards
for _ in range(number_of_cards):
# Find the next unrevealed card
while revealed[current_index % number_of_cards]:
current_index += 1
current_index %= number_of_cards
revealed_cards.append(arrangement_list[current_index])
revealed[current_index] = True
current_index += 1
sorted_deck = sorted(deck)
#If we reveal the cards in increasing order then this is a valid arrangement
if revealed_cards == sorted_deck:
valid_arrangements.append(arrangement_list)
return valid_arrangements
The core idea is to simulate the card revealing process backwards. We'll figure out the final card order by figuring out the order we'd place them if we knew how the deck was shuffled.
Here's how the algorithm would work step-by-step:
def reveal_cards_in_increasing_order(deck_of_cards):
number_of_cards = len(deck_of_cards)
result = [None] * number_of_cards
card_indices = list(range(number_of_cards))
sorted_cards = sorted(deck_of_cards)
# Process cards in decreasing order
for card in reversed(sorted_cards):
current_index = card_indices.pop(0)
result[current_index] = card
# Simulate discard and reveal
if card_indices:
card_indices.append(card_indices.pop(0))
return result
Case | How to Handle |
---|---|
Null input array | Throw IllegalArgumentException or return null/empty list based on the API contract. |
Empty input array | Return an empty list, as there are no cards to reveal. |
Input array with a single element | Return the array containing the single element directly. |
Input array with all identical values | The algorithm should correctly reveal these identical values in the specified order. |
Input array already sorted in ascending order | The algorithm should correctly handle this case and produce the reveal order. |
Input array sorted in descending order | The algorithm should correctly rearrange the cards to reveal in increasing order. |
Maximum-sized input array (considering memory constraints) | Ensure that the algorithm's memory usage remains within acceptable limits, potentially using an in-place algorithm if applicable. |
Array contains duplicate numbers | The algorithm should correctly handle duplicates, revealing them in the appropriate order based on the reshuffling process. |