Skip to content

2293. Min Max Game 👍

  • Time: $O(\log n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)