Reveal Cards In Increasing Order — LeetCode 950 Python Solution

MediumQueueArraySortingSimulation
Problem
#950
Pattern
Stack
Reading time
2 min

The problem

You are given an integer array deck. There is a deck of cards where every card has a unique integer.

Example

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.

Python solution

Python
class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
        q = deque()
        for v in sorted(deck, reverse=True):
            if q:
                q.appendleft(q.pop())
            q.appendleft(v)
        return list(q)

Complexity

MeasureComplexity
TimeO(V+E)
SpaceO(V) auxiliary

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 950. Reveal Cards In Increasing Order is filed here because LeetCode tags it Queue, which is the vocabulary this hub collects.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 950. Reveal Cards In Increasing Order?
LeetCode 950. Reveal Cards In Increasing Order is rated Medium on LeetCode.
What is the time complexity of LeetCode 950. Reveal Cards In Increasing Order?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 950. Reveal Cards In Increasing Order?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 950. Reveal Cards In Increasing Order cover?
LeetCode 950. Reveal Cards In Increasing Order is tagged Queue, Array, Sorting and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview