#2293

Min Max Game

Easy
ArraySimulationArraySimulation
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

The optimal solution uses the same simulation approach but recognizes that we can directly compute the last remaining number without storing intermediate arrays, reducing space usage.

⚙️

Algorithm

4 steps
  1. 1Step 1: Initialize a variable to hold the current index.
  2. 2Step 2: Iterate through the nums array, applying min/max operations directly in a single pass.
  3. 3Step 3: Keep reducing the size of the array until only one element remains.
  4. 4Step 4: Return the last remaining number.
solution.py11 lines
1# Full working Python code
2
3def minMaxGame(nums):
4    while len(nums) > 1:
5        for i in range(len(nums) // 2):
6            if i % 2 == 0:
7                nums[i] = min(nums[2 * i], nums[2 * i + 1])
8            else:
9                nums[i] = max(nums[2 * i], nums[2 * i + 1])
10        nums = nums[:len(nums) // 2]
11    return nums[0]

Complexity note: This complexity is linear because we are processing each element of the array a constant number of times, and we do not use any extra space proportional to the input size.

  • 1The algorithm reduces the size of the array exponentially, leading to a logarithmic number of iterations.
  • 2Understanding the pattern of min/max operations helps in optimizing the solution.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.