A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 486. Predict the Winner, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
The notes already sitting in the source point us in the right direction:
- recursion + memo
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are scoreInRange, PredictTheWinner, winner.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//recursion + memo
02//Runtime: 4 ms, faster than 46.97% of C++ online submissions for Predict the Winner.
03//Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for Predict the Winner.
04class Solution {
05public:
06 vector<int> nums;
07 vector<vector<int>> memo;
08
09 int scoreInRange(int i, int j){
10 //i, j are inclusive
11 if(j - i + 1 == 2) return max({nums[i], nums[j]});
12 if(memo[i][j] != -1) return memo[i][j];
13
14 memo[i][j] = max(
15 nums[i] + accumulate(nums.begin()+i+1, nums.begin()+j+1, 0) - scoreInRange(i+1, j),
16 nums[j] + accumulate(nums.begin()+i, nums.begin()+j, 0) - scoreInRange(i, j-1)
17 );
18
19 return memo[i][j];
20 };
21
22 bool PredictTheWinner(vector<int>& nums) {
23 int n = nums.size();
24 if(n <= 2) return true;
25 this->nums = nums;
26
27 memo = vector<vector<int>>(n, vector<int>(n, -1));
28
29 scoreInRange(0, n-1);
30
31 return memo[0][n-1] >= accumulate(nums.begin(), nums.end(), 0)/2.0;
32 }
33};
34
35//DP
36//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Predict the Winner.
37//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Predict the Winner.
38class Solution {
39public:
40 bool PredictTheWinner(vector<int>& nums) {
41 int n = nums.size();
42 if(n <= 2) return true;
43
44 vector<vector<int>> dp = vector<vector<int>>(n, vector<int>(n, -1));
45
46 for(int dist = 1; dist < n; dist++){
47 for(int i = 0; i+dist < n; i++){
48 int j = i+dist;
49 if(dist == 1){
50 dp[dist][i] = max(nums[i], nums[j]);
51 }else{
52 dp[dist][i] = max(
53 nums[i] + accumulate(nums.begin()+i+1, nums.begin()+j+1, 0)-dp[dist-1][i+1],
54 nums[j] + accumulate(nums.begin()+i, nums.begin()+j,0) - dp[dist-1][i]);
55 }
56 }
57 }
58
59 return dp[n-1][0] >= accumulate(nums.begin(), nums.end(), 0)/2.0;
60 }
61};
62
63//Approach #1 Recursion, Min-Max algorithm
64//Runtime: 276 ms, faster than 5.16% of C++ online submissions for Predict the Winner.
65//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Predict the Winner.
66//time: O(2^n), space: O(n)
67class Solution {
68public:
69 int winner(vector<int>& nums, int s, int e, int turn){
70 if(s == e) return turn * nums[s];
71 int a = turn * nums[s] + winner(nums, s+1, e, -turn);
72 int b = turn * nums[e] + winner(nums, s, e-1, -turn);
73 /*
74 for player2, it equals to -max(-a,-b) = min(a, b)
75 it want to minimize the score
76 */
77 return turn * max(turn*a, turn*b);
78 };
79
80 bool PredictTheWinner(vector<int>& nums) {
81 //it calculates Player1's score - Player2's score
82 return winner(nums, 0, nums.size()-1, 1) >= 0;
83 }
84};
85
86//Approach #2 Similar Approach, Recursion, min-max, memorization
87//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Predict the Winner.
88//Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Predict the Winner.
89//time: O(n^2), space: O(n^2)
90class Solution {
91public:
92 vector<vector<int>> memo;
93
94 int winner(vector<int>& nums, int s, int e){
95 if(memo[s][e] != -1) return memo[s][e];
96 if(s == e){
97 memo[s][e] = nums[s];
98 return memo[s][e];
99 }
100 //score's definition is still player1' score - player2's score
101 //notice the minus sign here
102 int a = nums[s] - winner(nums, s+1, e);
103 int b = nums[e] - winner(nums, s, e-1);
104 memo[s][e] = max(a, b);
105 return memo[s][e];
106 };
107
108 bool PredictTheWinner(vector<int>& nums) {
109 int n = nums.size();
110 memo = vector(n, vector(n, -1));
111
112 winner(nums, 0, n-1);
113 return memo[0][n-1] >= 0;
114 }
115};
116
117//Approach #3 O(n^2) space Dynamic Programming
118//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Predict the Winner.
119//Memory Usage: 6.6 MB, less than 100.00% of C++ online submissions for Predict the Winner.
120//time: O(n^2), space: O(n^2)
121class Solution {
122public:
123 bool PredictTheWinner(vector<int>& nums) {
124 int n = nums.size();
125 vector<vector<int>> dp = vector(n, vector(n, 0));
126
127 for(int s = n-1; s >= 0; s--){
128 for(int e = s+1; e < n; e++){
129 /*
130 if e == s+1, dp[s+1][e] and dp[s][e-1] equal to 0,
131 so a = nums[s] and b = nums[e],
132 this is edge case
133 */
134 /*
135 note that we use dp[s+1] here,
136 so we should iterate s in reverse order
137 */
138 int a = nums[s] - dp[s+1][e];
139 /*
140 note that we use dp[s][e-1] here,
141 so we should iterate e in increasing order
142 */
143 int b = nums[e] - dp[s][e-1];
144 dp[s][e] = max(a, b);
145 }
146 }
147
148 return dp[0][n-1] >= 0;
149 }
150};
151
152//Approach #4 O(n) space Dynamic Programming
153//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Predict the Winner.
154//Memory Usage: 6.1 MB, less than 100.00% of C++ online submissions for Predict the Winner.
155//time: O(n^2), space: O(n)
156class Solution {
157public:
158 bool PredictTheWinner(vector<int>& nums) {
159 int n = nums.size();
160 vector<int> dp(n, 0);
161 for(int s = n-2; s >= 0; s--){
162 for(int e = s+1; e < n; e++){
163 //dp[e] is dp[s+1][e] in previous approach
164 int a = nums[s] - dp[e];
165 /*
166 dp[e-1] is dp[s][e-1] in previous approach,
167 because we increase e in each iteration,
168 when we are calculating dp[e], dp[e-1] is already calculated
169 */
170 int b = nums[e] - dp[e-1];
171 //we can overwrite the only row
172 dp[e] = max(a, b);
173 }
174 }
175 return dp[n-1] >= 0;
176 }
177};
Cost