2293. Min Max Game LeetCode Solution

In this guide, you will get 2293. Min Max Game LeetCode Solution with the best time and space complexity. The solution to Min Max Game problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Min Max Game solution in C++
  4. Min Max Game solution in Java
  5. Min Max Game solution in Python
  6. Additional Resources
2293. Min Max Game LeetCode Solution image

Problem Statement of Min Max Game

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. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n / 2.
For every even index i where 0 <= i < n / 2, assign the value of newNums[i] as min(nums[2 * i], nums[2 * i + 1]).
For every odd index i where 0 <= i < n / 2, assign the value of newNums[i] as max(nums[2 * i], nums[2 * i + 1]).
Replace the array nums with newNums.
Repeat the entire process starting from step 1.

Return the last number that remains in nums after applying the algorithm.

Example 1:

Input: nums = [1,3,5,2,4,8,2,2]
Output: 1
Explanation: The following arrays are the results of applying the algorithm repeatedly.
First: nums = [1,5,4,2]
Second: nums = [1,4]
Third: nums = [1]
1 is the last remaining number, so we return 1.

Example 2:

Input: nums = [3]
Output: 3
Explanation: 3 is already the last remaining number, so we return 3.

See also  483. Smallest Good Base LeetCode Solution

Constraints:

1 <= nums.length <= 1024
1 <= nums[i] <= 109
nums.length is a power of 2.

Complexity Analysis

  • Time Complexity: O(\log n)
  • Space Complexity: O(n)

2293. Min Max Game LeetCode Solution in C++

class Solution {
 public:
  int minMaxGame(vector<int>& nums) {
    if (nums.size() == 1)
      return nums[0];

    vector<int> nextNums;
    for (int i = 0; i < nums.size() / 2; ++i)
      nextNums.push_back(i % 2 == 0 ? min(nums[2 * i], nums[2 * i + 1])
                                    : max(nums[2 * i], nums[2 * i + 1]));
    return minMaxGame(nextNums);
  }
};
/* code provided by PROGIEZ */

2293. Min Max Game LeetCode Solution in Java

class Solution {
  public int minMaxGame(int[] nums) {
    if (nums.length == 1)
      return nums[0];

    int[] nextNums = new int[nums.length / 2];
    for (int i = 0; i < nextNums.length; ++i)
      nextNums[i] = i % 2 == 0 ? Math.min(nums[2 * i], nums[2 * i + 1])
                               : Math.max(nums[2 * i], nums[2 * i + 1]);
    return minMaxGame(nextNums);
  }
}
// code provided by PROGIEZ

2293. Min Max Game LeetCode Solution in Python

class Solution:
  def minMaxGame(self, nums: list[int]) -> int:
    if len(nums) == 1:
      return nums[0]

    nextNums = []
    for i in range(len(nums) // 2):
      nextNums.append(min(nums[2 * i], nums[2 * i + 1]) if i % 2 == 0 else
                      max(nums[2 * i], nums[2 * i + 1]))
    return self.minMaxGame(nextNums)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.