File: min-max-game/solution.py

Date: 2026-06-06

Time: 17:50

min-max-game/solution.py

Purpose

This file solves LeetCode 2293 — Min Max Game. It simulates a tournament-style reduction of an array: pair up adjacent elements, alternately take the min or max of each pair, and repeat until one element remains.

Key Components

min_steps(nums: List[int]) -> int — Despite the name suggesting "steps," this function returns the *last remaining value*, not a count. It takes an array whose length is a power of 2 and reduces it by half each round until a single element is left.

The pairing rule per round:

Patterns

Iterative simulation — Rather than using recursion or a mathematical shortcut, it directly simulates the game by building a new half-sized array each round. This is the most straightforward approach: O(n) total work across all rounds (n + n/2 + n/4 + ... = 2n), O(n) space for the working array.

In-place reassignmentnums = new_nums at the end of each iteration replaces the old array, letting the GC reclaim previous rounds. No mutation of the input.

Dependencies

Flow

1. Enter the while loop — runs log2(len(nums)) times.

2. Each iteration pairs elements (nums[0], nums[1]), (nums[2], nums[3]), etc.

3. For pair index i: apply min if i is even, max if i is odd.

4. Collect results into new_nums, reassign nums.

5. When len(nums) == 1, return nums[0].

For [1, 3, 5, 2, 4, 8, 2, 2]:

Invariants

Error Handling

None. The function trusts its caller to provide a valid power-of-2-length array. Invalid inputs produce silent wrong answers or index errors rather than descriptive exceptions. This is typical for LeetCode solutions where the problem guarantees valid input.

Naming Note

The function is named min_steps, which is misleading — it returns the surviving *value*, not a step count. This likely came from a naming template applied across the repo and wasn't corrected for this problem.

Topics to Explore

Beliefs