Show problems as
Classic problem #950MediumArrayQueueSortingSimulation

Reveal Cards In Increasing Order

Real-world version: Stacking the Tags to Deal Them in Order
Tracked in this browser only
Read Reveal Cards In Increasing Order on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A stack of tags is held face down and dealt like this, over and over until nothing is left:

  • Take the tag at the top of the stack and set it aside.
  • If any tags remain, move the tag now at the top to the bottom of the stack.

Return an arrangement of the very same tags, listed from top to bottom, whose dealing sets them aside in increasing order. Exactly one arrangement does that.

Examples

Example 1

Input
tags = [1, 2, 3, 4]
Output
[1, 3, 2, 4]

Dealing this arrangement sets aside 1, moves 3 to the bottom, sets aside 2, moves 4 to the bottom, sets aside 3 and finally sets aside 4.

Example 2

Input
tags = [5]
Output
[5]

One tag is set aside straight away with nothing to move.

Example 3

Input
tags = [2, 1]
Output
[1, 2]

The smaller tag goes on top and is set aside first, then the other is moved to the bottom and set aside next.

Constraints

  • 1 <= tags.length <= 1000
  • 1 <= tags[i] <= 10^6
  • no two tags are the same

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def deck_revealed_increasing(tags: list[int]) -> list[int]:
Java
public int[] deckRevealedIncreasing(int[] tags)