forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min-max-game.cpp
33 lines (31 loc) · 847 Bytes
/
min-max-game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Time: O(n)
// Space: O(n)
// simulation, optimized from solution2
class Solution {
public:
int minMaxGame(vector<int>& nums) {
for (int n = size(nums); n != 1; n /= 2) {
for (int i = 0; i < n / 2; ++i) {
nums[i] = (i % 2 == 0 ? min(nums[2 * i], nums[2 * i + 1]) : max(nums[2 * i], nums[2 * i + 1]));
}
}
return nums[0];
}
};
// Time: O(n)
// Space: O(n)
// simulation
class Solution2 {
public:
int minMaxGame(vector<int>& nums) {
vector<int> q(nums);
while (size(q) != 1) {
vector<int> new_q;
for (int i = 0; i < size(q) / 2; ++i) {
new_q.emplace_back(i % 2 == 0 ? min(q[2 * i], q[2 * i + 1]) : max(q[2 * i], q[2 * i + 1]));
}
q = move(new_q);
}
return q[0];
}
};