Skip to content

2974. Minimum Number Game 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  vector<int> numberGame(vector<int>& nums) {
    ranges::sort(nums);
    for (int i = 0; i < nums.size(); i += 2)
      swap(nums[i], nums[i + 1]);
    return nums;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int[] numberGame(int[] nums) {
    Arrays.sort(nums);

    for (int i = 0; i < nums.length; i += 2) {
      final int temp = nums[i];
      nums[i] = nums[i + 1];
      nums[i + 1] = temp;
    }

    return nums;
  }
}
1
2
3
4
5
6
class Solution:
  def numberGame(self, nums: list[int]) -> list[int]:
    nums.sort()
    return [nums[i + 1] if i % 2 == 0
            else nums[i - 1]
            for i in range(len(nums))]