← Home

1025. Divisor Game

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
102

Let's make this one less mysterious. For 1025. Divisor Game, the solution in this repository is mainly a greedy solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. 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: greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are getDivisors, divisorGame, win.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • 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:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

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

solution.cpp
01//Runtime: 8 ms, faster than 15.67% of C++ online submissions for Divisor Game.
02//Memory Usage: 9.7 MB, less than 6.90% of C++ online submissions for Divisor Game.
03class Solution {
04public:
05    vector<int> getDivisors(int x){
06        vector<int> divisors;
07        
08        for(int divisor = x/2; divisor >= 1; divisor--){
09            if(x % divisor == 0){
10                divisors.push_back(divisor);
11            }
12        }
13        
14        return divisors;
15    }
16    
17    bool divisorGame(int N) {
18        vector<bool> win(N+1, false);
19        
20        win[1] = false;
21        win[2] = true;
22        
23        for(int i = 3; i <= N; i++){
24            vector<int> divisors = getDivisors(i);
25            // cout << "i: " << i << endl;
26            // copy(divisors.begin(), divisors.end(), ostream_iterator<int>(cout, " "));
27            // cout << endl;
28            // cout << "win: " << win[i] << endl;
29            for(int divisor : divisors){
30                if(!win[i - divisor]){
31                    win[i] = true;
32                    break;
33                }
34            }
35            // cout << "win: " << win[i] << endl;
36        }
37        
38        return win[N];
39    }
40};
41
42//make all_divisors an array of array
43//Runtime: 20 ms, faster than 6.95% of C++ online submissions for Divisor Game.
44//Memory Usage: 10.6 MB, less than 6.90% of C++ online submissions for Divisor Game.
45class Solution {
46public:
47    vector<vector<int>> all_divisors;
48        
49    vector<int> getDivisors(int x){
50        vector<int> divisors;
51        
52        for(int divisor = x/2; divisor >= 1; divisor--){
53            if(x % divisor == 0){
54                if(divisor == 1){
55                    divisors.push_back(divisor);
56                }else{
57                    if(divisor == x / divisor){
58                        divisors = all_divisors[divisor];
59                        divisors.push_back(divisor);
60                    }else{
61                        vector<int> a = all_divisors[divisor];
62                        vector<int> b = all_divisors[x / divisor];
63                        sort(a.begin(), a.end());
64                        sort(b.begin(), b.end());
65                        divisors = vector<int>(a.size() + b.size());
66                        std::vector<int>::iterator it = std::set_union(a.begin(), a.end(), b.begin(), b.end(), divisors.begin());
67                        divisors.resize(it - divisors.begin());
68                        divisors.push_back(divisor);
69                        divisors.push_back(x / divisor);
70                    }
71                    
72                }
73                break;
74            }
75        }
76        return divisors;
77    }
78    
79    bool divisorGame(int N) {
80        vector<bool> win(N+1, false);
81        all_divisors = vector<vector<int>>(N+1);
82        
83        win[1] = false;
84        
85        for(int i = 2; i <= N; i++){
86            cout << "i: " << i << endl;
87            all_divisors[i] = getDivisors(i);
88            copy(all_divisors[i].begin(), all_divisors[i].end(), ostream_iterator<int>(cout, " "));
89            cout << endl;
90            // cout << "win: " << win[i] << endl;
91            //if it can find a divisor so that win[i-divisor] is false, i will win
92            for(int divisor : all_divisors[i]){
93                if(!win[i - divisor]){
94                    win[i] = true;
95                    break;
96                }
97            }
98            // cout << "win: " << win[i] << endl;
99        }
100        
101        return win[N];
102    }
103};
104
105//https://leetcode.com/problems/divisor-game/discuss/274566/just-return-N-2-0-(proof)
106//Runtime: 4 ms, faster than 61.78% of C++ online submissions for Divisor Game.
107//Memory Usage: 8.1 MB, less than 96.55% of C++ online submissions for Divisor Game.
108class Solution {
109public:
110    bool divisorGame(int N) {
111        return N%2 == 0;
112    }
113};
114
115//Runtime: 4 ms, faster than 61.78% of C++ online submissions for Divisor Game.
116//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for Divisor Game.
117class Solution {
118public:
119    bool divisorGame(int N) {
120        return !(N & 1);
121    }
122};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.