Leetcode #950: Reveal Cards In Increasing Order
In this guide, we solve Leetcode #950 Reveal Cards In Increasing Order in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given an integer array deck. There is a deck of cards where every card has a unique integer.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Queue, Array, Sorting, Simulation
Intuition
We need level-order exploration or shortest-step expansion, which maps directly to a queue.
BFS guarantees the first time you reach a node is the shortest in unweighted graphs.
Approach
Initialize the queue with starting nodes and expand outward layer by layer.
Track visited nodes to avoid cycles and redundant work.
Steps:
- Initialize a queue with start nodes.
- Pop, process, and enqueue neighbors.
- Track visited nodes.
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.
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.
Python Solution
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
The time complexity is O(V+E). The space complexity is O(V).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.