Problem 950

Abstract / Summary

Arrange a deck so that revealing and moving cards in a specific way results in them being shown in increasing order.

Note

The full problem description, including helpful examples, can be found on LeetCode.

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
10 lines | 291 bytes | submitted November 23, 2023 | GitHub   12% comment ratio | tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 84% (200k+ submissions).

Typical patterns to solve this problem:
  • Array
  • Queue
  • Simulation
  • Sorting
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
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.
LeetCode profile | GitHub repo

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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 7 | μ = 7.26 | σ = 0.89

Complexity Analysis

not analyzed yet

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
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#
30 links | 18 authors | 8 languages

Videos

4 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

15 solutions   November 23, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • 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