← Home

950. Reveal Cards In Increasing Order

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
950

Let's make this one less mysterious. For 950. Reveal Cards In Increasing Order, the solution in this repository is mainly a greedy solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are deckRevealedIncreasing.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02In a deck of cards, every card has a unique integer.  You can order the deck in any order you want.
03
04Initially, all the cards start face down (unrevealed) in one deck.
05
06Now, you do the following steps repeatedly, until all cards are revealed:
07
08Take the top card of the deck, reveal it, and take it out of the deck.
09If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
10If there are still unrevealed cards, go back to step 1.  Otherwise, stop.
11Return an ordering of the deck that would reveal the cards in increasing order.
12
13The first entry in the answer is considered to be the top of the deck.
14
15 
16
17Example 1:
18
19Input: [17,13,11,2,3,5,7]
20Output: [2,13,3,11,5,17,7]
21Explanation: 
22We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
23After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
24We reveal 2, and move 13 to the bottom.  The deck is now [3,11,5,17,7,13].
25We reveal 3, and move 11 to the bottom.  The deck is now [5,17,7,13,11].
26We reveal 5, and move 17 to the bottom.  The deck is now [7,13,11,17].
27We reveal 7, and move 13 to the bottom.  The deck is now [11,17,13].
28We reveal 11, and move 17 to the bottom.  The deck is now [13,17].
29We reveal 13, and move 17 to the bottom.  The deck is now [17].
30We reveal 17.
31Since all the cards revealed are in increasing order, the answer is correct.
32 
33
34Note:
35
361 <= A.length <= 1000
371 <= A[i] <= 10^6
38A[i] != A[j] for all i != j
39**/
40
41//Your runtime beats 21.79 % of cpp submissions.
42/**
43Approach 1: Simulation
44Intuition and Algorithm
45
46Simulate the revealing process with a deck set to [0, 1, 2, ...]. 
47If for example this deck is revealed in the order [0, 2, 4, ...] then we know we need to put the smallest card in index 0,
48the second smallest card in index 2, the third smallest card in index 4, etc.
49**/
50class Solution {
51public:
52    vector<int> deckRevealedIncreasing(vector<int>& deck) {
53        vector<int> index;
54        for(int i = 0; i < deck.size(); i++){
55            index.push_back(i);
56        }
57        
58        sort(deck.begin(), deck.end());
59        
60        vector<int> ans(deck.size());
61        for(int i = 0; i < deck.size(); i++){
62            ans[index.front()] = deck[i];
63            index.erase(index.begin());
64            if(!index.empty()){
65                index.push_back(index.front());
66                index.erase(index.begin());
67            }
68        }
69        
70        return ans;
71    }
72};
73
74/**
75Complexity Analysis
76
77Time Complexity: O(NlogN), where N is the length of deck.
78
79Space Complexity: O(N). 
80**/

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.