Minimum Number Game (LeetCode 2974): Solution and Explanation

Easy
Array
Sorting
Heap (priority queue)
Simulation

Alice and Bob repeatedly remove the two smallest numbers, and Bob writes his number down before Alice writes hers. Return the list they write.

Intuition

Each round takes the smallest remaining value (Alice) and then the next smallest (Bob). After the round those two are gone, so the next round takes the next two smallest. The rounds simply walk through the numbers in sorted order, two at a time.

Inside a round Alice's number is never larger than Bob's. Bob appends first, so every pair lands in arr as larger then smaller.

That means the answer is the sorted array with every adjacent pair swapped: positions 0 and 1 trade places, 2 and 3 trade places, and so on.

You can also simulate the game literally with a min-heap: pop twice, push Bob's value to the output, then Alice's. It gives the same result with the same O(n log n) bound.

Approach: Sort, then swap each pair

  1. Sort nums in ascending order.
  2. Walk i = 0, 2, 4, ...: alice = nums[i], bob = nums[i + 1].
  3. Append bob, then alice, to arr.
  4. Return arr.

Step-by-step example

nums = [5,4,2,3]
  1. Sort: [2,3,4,5].
  2. Round 1: Alice takes 2, Bob takes 3. Bob writes 3, Alice writes 2. arr = [3,2].
  3. Round 2: Alice takes 4, Bob takes 5. arr = [3,2,5,4].
  4. The table is empty, so the answer is [3,2,5,4].

Code: Python, Java, C++

1class Solution:
2    def numberGame(self, nums: List[int]) -> List[int]:
3        nums.sort()
4        arr = []
5        for i in range(0, len(nums), 2):
6            alice = nums[i]
7            bob = nums[i + 1]
8            arr.append(bob)
9            arr.append(alice)
10        return arr
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting sort O(n + V)
Sort, then swap each pair O(n log n)
Min-heap simulation O(n log n)
Time

O(n log n)

Sorting dominates; the pair loop is O(n).

Space

O(n)

The output array holds n values. Sorting in place adds at most O(log n) beyond that.

Other approaches

Min-heap simulation (time O(n log n), space O(n))

Heapify nums, then each round pop Alice's value and Bob's value and append Bob's first. This mirrors the statement exactly.

Counting sort (time O(n + V), space O(V))

Values are between 1 and 100, so a frequency table can produce the sorted order in O(n + 100) before swapping pairs.

Common mistakes

  • Appending Alice's value first. Bob writes first, so each pair goes in larger then smaller.
  • Using the default JavaScript sort, which compares numbers as strings. Pass a comparator such as (a, b) => a - b.
  • Searching for the minimum with a fresh scan every round. It works for n = 100 but is O(n^2) and misses the simple sorted pattern.

Frequently asked questions

Why is the answer just the sorted array with pairs swapped?

Each round removes the two smallest remaining values, so round r gets sorted positions 2r and 2r+1. Bob holds the larger one and writes it first.

What happens when Alice and Bob take equal values?

Nothing special. Both values are the same, so swapping them leaves the pair unchanged.

Is a heap needed?

No. A heap simulates the rounds directly, but sorting once gives the same order and is simpler.

What is the complexity?

O(n log n) time for the sort and O(n) space for the output.