Problem 2293

Abstract / Summary

Players repeatedly replace each pair of numbers with their minimum and maximum until one number remains.

Note

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

Python 2293.py

class Solution:
    def minMaxGame(self, nums: List[int]) -> int:
        size = len(nums)

        # basic cases
        if size == 1:
            return nums[0]
        if size == 2:
            return min(nums)

        # general case
        next = []
        for i in range(0, size, 4):
            next.append(min(nums[  i  ], nums[i + 1]))
            next.append(max(nums[i + 2], nums[i + 3]))

        # recursion
        return self.minMaxGame(next)
18 lines | 478 bytes | submitted February 3, 2024 | GitHub   25% comment ratio | tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 64% (100k+ submissions).

Typical patterns to solve this problem:
  • Array
  • Simulation
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 defines a class Solution with a single method minMaxGame. The method receives a list of integers and recursively reduces the list by alternating minimum and maximum operations until a single value remains, which is then returned. The algorithm follows a divide-and-conquer pattern, processing the input in fixed-size blocks and invoking itself on the reduced list.

Key Steps

Determining the size of the input

size = len(nums)

The length of the incoming list is stored in a local variable. This value drives the subsequent control flow and loop bounds.

Handling the smallest inputs

if size == 1:
    return nums[0]
if size == 2:
    return min(nums)

When the list contains a single element, the method returns that element directly. For a two-element list, the smaller of the two numbers is returned. These branches terminate the recursion early for trivial cases.

Building the next level of reduction

next = []
for i in range(0, size, 4):
    next.append(min(nums[  i  ], nums[i + 1]))
    next.append(max(nums[i + 2], nums[i + 3]))

The core of the algorithm iterates over the original list in steps of four. For each block of four consecutive numbers, the first two are reduced to their minimum, and the next two are reduced to their maximum. The results are appended to a new list called next. This list represents the state of the game after one round of operations.

Recursing on the reduced list

return self.minMaxGame(next)

After constructing the next list, the method calls itself with this new list, continuing the reduction process until one of the base cases is reached.

Edge Cases Handled

The explicit checks for size == 1 and size == 2 ensure that the function behaves correctly when the input cannot be partitioned into groups of four. By returning the appropriate value for these minimal lengths, the recursion is guaranteed to terminate without attempting out-of-range indexing.

The loop increments by four and accesses indices i, i+1, i+2, and i+3. Because the base cases filter out lengths that are not multiples of four, the loop never encounters an index beyond the bounds of the list. This implicit guarantee prevents index errors for inputs whose size is a power of two, which is the expected domain for the problem.

Performance Optimizations

The algorithm processes the list in place with a single pass per recursion level, constructing the next list using append operations that amortize allocation costs. By reducing the problem size by a factor of two on each recursive call (each four-element block yields two results), the total number of processed elements across all levels forms a geometric series, leading to efficient overall execution.

The use of local variables (size, next) and direct indexing avoids the overhead of higher-level abstractions such as slicing or comprehensions inside the loop. This tight loop structure minimizes Python bytecode execution time for each iteration.

The early return for the two-element case eliminates an unnecessary recursive call when only a single reduction step is required, saving both call-stack depth and function-call overhead.

written by openai gpt

ELI5 - Simplified Explanation

Imagine you have a long row of number blocks laid out on the floor. To solve this puzzle, you go through the row in little groups of four blocks at a time. For the first two blocks in each group, you look at them and keep only the smaller one. For the next two blocks in that same group, you look at them and keep only the bigger one. You take all these winners and line them up to make a new, shorter row of blocks. Because the new row is smaller, you just repeat the exact same game with those leftovers. You keep doing this over and over again until eventually there is only one lucky block standing all by itself, which is the secret answer to the puzzle.

written by google gemini

Code Quality

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

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

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 Java Python C++ TS
WalkCCC Java Python C++
leetcode.ca Java Python C++ C Go Rust TS
Progiez Java Python C++
github charles-wangkai Java
everthis JS
fartem Ruby
fsq C++
JoshCrozier JS
lkwq007 Python
varunu28 Java
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C, C++, Go, Java, Python, Rust, TypeScript
Progiez C++, Java, Python
charles-wangkai Java
everthis JavaScript
fartem Ruby
fsq C++
JoshCrozier JavaScript
lkwq007 Python
varunu28 Java
24 links | 11 authors | 9 languages

Videos

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

Similar Problems

1 similar problem

Solved On The Same Day

5 solutions   February 3, 2024

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