forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stone-game-ix.cpp
34 lines (33 loc) · 1.26 KB
/
stone-game-ix.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
34
// Time: O(n)
// Space: O(1)
class Solution {
public:
bool stoneGameIX(vector<int>& stones) {
unordered_map<int, int> count;
for (const auto& x : stones) {
++count[x % 3];
}
if (count[0] % 2 == 0) {
// iff both counts are not zero, then alice takes the least one at first, the remains are deterministic for bob to lose:
// - assumed count[1] is the least one:
// 1(,1,2)*,2,(,2)* => bob loses
// ^
// - assumed count[2] is the least one:
// 2(,2,1)*,1,(,1)* => bob loses
// ^
return count[1] && count[2];
}
// iff abs(count[1]-count[2]) >= 3, then alice takes the most one at first, the remains are deterministic for bob to lose:
// - assumed count[1] is the most one
// 1(,1,2)*,0,1(,2,1)*,1,(,1)* => bob loses
// ^
// 1(,1,2)*,1,0,1,(,1)* => bob loses
// ^
// - assumed count[2] is the most one
// 2(,2,1)*,0,2(,1,2)*,2,(,2)* => bob loses
// ^
// 2(,2,1)*,2,0,2,(,2)* => bob loses
// ^
return abs(count[1] - count[2]) >= 3;
}
};