-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoneGameVII.cpp
127 lines (114 loc) · 6 KB
/
stoneGameVII.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* dp[leftmostStoneIdx][rightmostStoneIdx][turn] stores the results of the already visited nodes.
*
* EXAMPLE: dp[10][34][0] = N, means that if Alice (turn==0) ends up in a situation where she has to choose
* between stone at idx 10 and stone at idx 34, and we have already got to compute the result N, because in the
* previous iterations we happend to go through the exact same situation, we will return N, instead of expanding the
* recursion further
*/
int dp[1001][1001][2] = {};
/*
* sumOfStonesBetween contains all the sub-arrays sums, thus speeding up the computation of the score's difference
*/
int sumOfStonesBetween[1001][1001] = {};
int game(int leftmostStoneIdx, int rightmostStoneIdx, int turn)
{
/*
* There's no remaining stone, exit condition
*/
if(leftmostStoneIdx >= rightmostStoneIdx) return 0;
/*
* If we have already happened to compute the result for the same situation, we return the previously computed result.
*/
if(dp[leftmostStoneIdx][rightmostStoneIdx][turn]!=0)
return dp[leftmostStoneIdx][rightmostStoneIdx][turn];
//Alice's turn
if(turn==0)
{
/*
* When Alice picks the rightmost stone, the final score difference increases by the sum of the remaining stones
* (sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx-1])
*/
int scoreDiffPickingRightmostStone =
sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx-1] +
game(leftmostStoneIdx, rightmostStoneIdx-1, 1);
/*
* When Alice picks the leftmost stone, the final score difference increases by the sum of the remaining stones
* (sumOfStonesBetween[leftmostStoneIdx+1][rightmostStoneIdx])
*/
int scoreDiffPickingLeftmostStone =
sumOfStonesBetween[leftmostStoneIdx+1][rightmostStoneIdx] +
game(leftmostStoneIdx+1, rightmostStoneIdx, 1);
/*
* Once computed the two different score's differences that Alice can get based on the stone she picked,
* she will keep only the result maximixing this difference.
* We store the solution in dp, so that we can reause it in case we come across the same situation later on.
*/
dp[leftmostStoneIdx][rightmostStoneIdx][0] = max(scoreDiffPickingRightmostStone, scoreDiffPickingLeftmostStone);
return dp[leftmostStoneIdx][rightmostStoneIdx][0];
}
//Bob's turn
else
{
/*
* When Bob picks the rightmost stone, the final score difference decreses by the sum of the remaining stones
* (sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx-1])
*/
int scoreDiffPickingRightmostStone =
game(leftmostStoneIdx, rightmostStoneIdx-1, 0) -
sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx-1];
/*
* When Bob picks the leftmost stone, the final score difference decreases by the sum of the remaining stones
* (sumOfStonesBetween[leftmostStoneIdx+1][rightmostStoneIdx])
*/
int scoreDiffPickingLeftmostStone =
game(leftmostStoneIdx+1, rightmostStoneIdx, 0) -
sumOfStonesBetween[leftmostStoneIdx+1][rightmostStoneIdx];
/*
* Once computed the two different score's differences that Bob can get based on the stone he picked,
* he will keep only the result minimizing this difference.
* We store the solution in dp, so that we can reause it in case we come across the same situation later on.
*/
dp[leftmostStoneIdx][rightmostStoneIdx][1] = min(scoreDiffPickingRightmostStone, scoreDiffPickingLeftmostStone);
return dp[leftmostStoneIdx][rightmostStoneIdx][1];
}
}
int stoneGameVII(vector<int>& stones)
{
/*
* Fill up the matrix sumOfStonesBetween[start][end] with the sums of the stones between 'start' and 'end'.
* This way we won't need to go through each remaining stone to compute the total sum every single time a player picks one.
* EXAMPLE: input [5,3,1,4,2]
* sumOfStonesBetween[][]:
*
* rightmostStone | 5 | 3 | 1 | 4 | 2 |
* leftmostStone |
* 5 | 5 | 8 | 9 | 13 | 15 |
* 3 | | 3 | 4 | 8 | 10 |
* 1 | | | 1 | 5 | 7 |
* 4 | | | | 4 | 6 |
* 2 | | | | | 2 |
*
* sumOfStonesBetween[1][3] = 8, meaning that given the input [5,3,1,4,2], considering the subarray between indexes 0 and 3 -
* [3,1,4] - the total sum of these stones is 8.
*/
for(int leftmostStoneIdx=0; leftmostStoneIdx<stones.size(); ++leftmostStoneIdx)
{
for(int rightmostStoneIdx=leftmostStoneIdx; rightmostStoneIdx<stones.size(); ++rightmostStoneIdx)
{
/*
* sumOfStonesBetween[IDX][IDX] = stones[IDX];
*/
if(leftmostStoneIdx == rightmostStoneIdx)
sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx] = stones[leftmostStoneIdx];
/*
* sumOfStonesBetween[A][B] = sumOfStonesBetween[A][B-1] + stones[B]
*/
else
sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx] =
sumOfStonesBetween[leftmostStoneIdx][rightmostStoneIdx-1] +
stones[rightmostStoneIdx];
}
}
return game(0, stones.size()-1, 0);
}