Minimum Number Game (LeetCode 2974): Solution and Explanation
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
- Sort nums in ascending order.
- Walk i = 0, 2, 4, ...: alice = nums[i], bob = nums[i + 1].
- Append bob, then alice, to arr.
- Return arr.
Step-by-step example
nums = [5,4,2,3]
- Sort: [2,3,4,5].
- Round 1: Alice takes 2, Bob takes 3. Bob writes 3, Alice writes 2. arr = [3,2].
- Round 2: Alice takes 4, Bob takes 5. arr = [3,2,5,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 arr1class Solution {
2 public int[] numberGame(int[] nums) {
3 Arrays.sort(nums);
4 int[] arr = new int[nums.length];
5 for (int i = 0; i < nums.length; i += 2) {
6 int alice = nums[i];
7 int bob = nums[i + 1];
8 arr[i] = bob;
9 arr[i + 1] = alice;
10 }
11 return arr;
12 }
13}1class Solution {
2public:
3 vector<int> numberGame(vector<int>& nums) {
4 sort(nums.begin(), nums.end());
5 vector<int> arr;
6 for (size_t i = 0; i < nums.size(); i += 2) {
7 int alice = nums[i];
8 int bob = nums[i + 1];
9 arr.push_back(bob);
10 arr.push_back(alice);
11 }
12 return arr;
13 }
14};Time and space complexity
O(n log n)
Sorting dominates; the pair loop is O(n).
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.