-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoneGameVI.cpp
38 lines (30 loc) · 1.22 KB
/
stoneGameVI.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
35
36
37
38
enum Player {Alice=0, Bob=1};
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues)
{
int numberOfStones = aliceValues.size();
int aliceScore = 0;
int bobScore = 0;
// Sort the stones by sum of bob and alice values
auto cmp = [&](const int s1, const int s2){
return aliceValues[s1]+bobValues[s1] > aliceValues[s2]+bobValues[s2];
};
// Create an array containing the stone indexes
// stoned:= [0, 1, 2, ..., numberOfStones]
vector<int> stones(numberOfStones);
iota(stones.begin(), stones.end(), 0);
// Sort the stones to have those with a bigger total value before
sort(stones.begin(), stones.end(), cmp);
// Alice starts first
Player player = Alice;
for(int i=0; i<stones.size(); ++i)
{
if(player==Alice)
aliceScore += aliceValues[stones[i]];
else
bobScore += bobValues[stones[i]];
player = (player==Alice)? Bob : Alice;
}
if(aliceScore == bobScore)
return 0;
return aliceScore>bobScore ? 1 : -1;
}