Python 0950.py
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
deck.sort()
# build in reverse order
seq = [ deck.pop() ]
while deck:
seq.insert(0, seq.pop())
seq.insert(0, deck.pop())
return seq
Difficulty
and has an acceptance rate of about 84% (200k+ submissions).
Typical patterns to solve this problem:
- Array
- Queue
- Simulation
- Sorting
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation solves the “deck revealed increasing” problem by first sorting the input list and then constructing the final ordering in reverse. The algorithm works by repeatedly moving elements between the front and back of a temporary sequence, effectively simulating the reveal process backwards. The result is a list that, when the described reveal operation is applied, yields the cards in increasing order.
Key Steps
The first operation sorts the input deck in ascending order, guaranteeing that the smallest values are processed last during the reverse construction.
deck.sort()
A new list named seq is initialized with the largest card, which is removed from the sorted deck using pop(). This establishes the base of the reverse-built sequence.
seq = [ deck.pop() ]
The core loop continues while there are still cards left in deck. Inside the loop two manipulations are performed on seq. The first moves the current last element of seq to the front, mimicking the reverse of the “reveal top card then move next top to bottom” step.
seq.insert(0, seq.pop())
The second inserts the next largest remaining card at the front of seq, preserving the reverse order needed for the final arrangement.
seq.insert(0, deck.pop())
When the loop terminates, seq contains the deck order that satisfies the problem’s condition and is returned directly.
return seq
Edge Cases
The algorithm naturally handles an empty input list because the initial sort produces an empty list, the seq initialization attempts to pop from an empty list would be avoided as the loop condition while deck: fails immediately, and the function returns an empty list. For a single-element deck, the sort leaves the element in place, seq is created with that sole element, the loop body never executes, and the single element is returned unchanged, which is the correct result. The use of pop() without an index always removes the last element, so the code correctly processes decks of any length without special casing.
Performance Optimizations
The implementation leverages in-place sorting, which avoids allocating additional structures for ordering the input. By building the result in reverse, the algorithm eliminates the need for forward simulation of the reveal process, reducing the number of list operations. The use of pop() on the end of a Python list is O(1), and insert(0, …) is used only twice per iteration, keeping the overhead predictable. The overall flow processes each card exactly once after sorting, ensuring that the work done after the initial sort is linear with respect to the number of cards. This approach yields a compact and efficient solution within the constraints of the language’s list operations.
ELI5 - Simplified Explanation
Imagine you have a deck of cards with numbers on them, and you want to arrange them in a very special way so that when you flip them over one by one, they show up in order from the smallest number to the biggest number.
First, the code takes all the cards and lines them up from smallest to largest so it knows which numbers are the most important. To figure out the right order, it starts by taking the biggest card and putting it down on the table all by itself.
Next, it looks at the cards that are still left in the messy pile. For every card left, it does a little dance. It takes the card that is already on the table, pulls it off, and moves it to the very back of the line so it can stay safe for a moment. Then, it takes the next biggest card from the pile and puts it right at the front of the line.
It keeps doing this dance over and over again until all the cards from the pile have been moved into the line. By shifting the cards around this way, the line slowly grows into the perfect secret order where every card will land exactly where it needs to be when the time comes to reveal them.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||
| www | AlgoMonster | C++ | Python | Java | TS | ||||
| WalkCCC | C++ | Python | Java | ||||||
| leetcode.ca | C++ | Python | Java | Go | |||||
| LeetcodeHelp | C++ | Python | Java | ||||||
| Progiez | C++ | Python | Java | ||||||
| github | BigEggStudy | C# | |||||||
| black-shadows | C++ | ||||||||
| charles-wangkai | Java | ||||||||
| everthis | JS | ||||||||
| fartem | Ruby | ||||||||
| fsq | C++ | ||||||||
| gouthampradhan | Java | ||||||||
| haoel | Python | ||||||||
| JoshCrozier | JS | ||||||||
| keineahnung2345 | C++ | ||||||||
| lkwq007 | Python | ||||||||
| lzl124631x | C++ | ||||||||
| suxrobGM | C# | ||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||
| WalkCCC | C++, Python, Java | ||||||||
| leetcode.ca | C++, Go, Java, Python | ||||||||
| LeetcodeHelp | C++, Java, Python | ||||||||
| Progiez | C++, Java, Python | ||||||||
| BigEggStudy | C# | ||||||||
| black-shadows | C++, C++ | ||||||||
| charles-wangkai | Java | ||||||||
| everthis | JavaScript | ||||||||
| fartem | Ruby | ||||||||
| fsq | C++ | ||||||||
| gouthampradhan | Java | ||||||||
| haoel | Python | ||||||||
| JoshCrozier | JavaScript | ||||||||
| keineahnung2345 | C++ | ||||||||
| lkwq007 | Python | ||||||||
| lzl124631x | C++ | ||||||||
| suxrobGM | C# |
Videos
Solved On The Same Day
- problem 2932: Maximum Strong Pair XOR I
- problem 731: My Calendar II
- problem 792: Number of Matching Subsequences
- problem 820: Short Encoding of Words
- problem 911: Online Election
- problem 1023: Camelcase Matching
- problem 1038: Binary Search Tree to Greater Sum Tree
- problem 1170: Compare Strings by Frequency of the Smallest Character
- problem 1171: Remove Zero Sum Consecutive Nodes from Linked List
- problem 1696: Jump Game VI
- problem 2327: Number of People Aware of a Secret
- problem 432: All O`one Data Structure
- problem 732: My Calendar III
- problem 1044: Longest Duplicate Substring
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality