The trick here is to name the state correctly, then let the implementation follow. For 1510. Stone Game IV, the solution in this repository is mainly a DFS + memoization solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: DFS + memoization, dynamic programming.
The notes already sitting in the source point us in the right direction:
- dfs + memorization
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are dfs, winnerSquareGame.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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//dfs + memorization
02//Runtime: 128 ms, faster than 60.00% of C++ online submissions for Stone Game IV.
03//Memory Usage: 12.3 MB, less than 100.00% of C++ online submissions for Stone Game IV.
04class Solution {
05public:
06 vector<int> memo;
07
08 bool dfs(int n){
09 // cout << "n: " << n << endl;
10 if(memo[n] != -1){
11 return memo[n];
12 }
13
14 for(int i = 1; i * i <= n; ++i){
15 if(!dfs(n - i*i)){
16 return memo[n] = true;
17 }
18 }
19
20 return memo[n] = false;
21 };
22
23 bool winnerSquareGame(int n) {
24 memo = vector<int>(n+1, -1);
25
26 memo[0] = 0;
27 for(int i = 1; i*i <= n; ++i){
28 memo[i*i] = 1;
29 }
30
31 return dfs(n);
32 }
33};
34
35//bottom-up DP
36//https://leetcode.com/problems/stone-game-iv/discuss/730582/JavaC%2B%2BPython-DP
37//Runtime: 80 ms, faster than 80.00% of C++ online submissions for Stone Game IV.
38//Memory Usage: 7.8 MB, less than 100.00% of C++ online submissions for Stone Game IV.
39class Solution {
40public:
41 bool winnerSquareGame(int n) {
42 vector<int> dp(n+1, false);
43
44 for(int i = 1; i <= n; ++i){
45 for(int k = 1; k * k <= i; ++k){
46 //there is a 'k' s.t. we can make the opponent lose
47 if(!dp[i - k*k]){
48 dp[i] = true;
49 break;
50 }
51 }
52 }
53
54 return dp[n];
55 }
56};
Cost