[LeetCode] Minimum Number Game

10035. Minimum Number Game

You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:

  • Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
  • Now, first Bob will append the removed element in the array arr, and then Alice does the same.
  • The game continues until nums becomes empty.

Return the resulting array arr.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
vector<int> numberGame(vector<int>& A) {
vector<int> res;
sort(begin(A), end(A));
for(int i = 0; i < A.size(); i+=2) {
res.push_back(A[i+1]);
res.push_back(A[i]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/24/PS/LeetCode/minimum-number-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.