Reveal Cards In Increasing Order

Medium Topics Company Tags

Reorder the distinct card values in deck so that the following process reveals them in increasing order:

  1. Reveal and remove the top card.
  2. If any cards remain, move the next top card to the bottom without revealing it.
  3. Repeat until the deck is empty.

Return the initial arrangement from top to bottom. The first array entry is the top card. Return the arrangement to start with, rather than the sequence of revealed values.

Example 1:

Input: deck = [4,1,3,2]

Output: [1,3,2,4]

Explanation: Start with [1,3,2,4]. Reveal 1 and move 3 to the bottom, leaving [2,4,3]. Reveal 2 and move 4 to the bottom, leaving [3,4]. The final reveals are 3 and 4, so the revealed values are increasing.

Example 2:

Input: deck = [9]

Output: [9]

Explanation: There is only one card. Placing 9 at the top reveals 9 immediately.

Example 3:

Input: deck = [8,3,5]

Output: [3,8,5]

Explanation: Starting with [3,8,5], reveal 3 and move 8 to the bottom. The remaining deck is [5,8], which reveals 5 and then 8.

Constraints:

  • 1 <= deck.length <= 1000
  • 1 <= deck[i] <= 1000000
  • All card values are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.