Min Max Game — LeetCode 2293 Python Solution

EasyArraySimulation
Problem
#2293
Reading time
2 min

The problem

You are given a 0-indexed integer array nums whose length is a power of 2. Apply the following algorithm on nums: Let n be the length of nums.

Example

Input
nums = [1,3,5,2,4,8,2,2]
Output
1
Explanation
The following arrays are the results of applying the algorithm repeatedly.

Python solution

Python
class Solution:
    def minMaxGame(self, nums: List[int]) -> int:
        n = len(nums)
        while n > 1:
            n >>= 1
            for i in range(n):
                a, b = nums[i << 1], nums[i << 1 | 1]
                nums[i] = min(a, b) if i % 2 == 0 else max(a, b)
        return nums[0]

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array \textit{nums}
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2293. Min Max Game?
LeetCode 2293. Min Max Game is rated Easy on LeetCode.
What is the time complexity of LeetCode 2293. Min Max Game?
The Python solution on this page runs in O(n), where n is the length of the array \textit{nums}.
What is the space complexity of LeetCode 2293. Min Max Game?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2293. Min Max Game cover?
LeetCode 2293. Min Max Game is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview