forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1025. Divisor Game.cpp
123 lines (106 loc) · 3.98 KB
/
1025. Divisor Game.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
//Runtime: 8 ms, faster than 15.67% of C++ online submissions for Divisor Game.
//Memory Usage: 9.7 MB, less than 6.90% of C++ online submissions for Divisor Game.
class Solution {
public:
vector<int> getDivisors(int x){
vector<int> divisors;
for(int divisor = x/2; divisor >= 1; divisor--){
if(x % divisor == 0){
divisors.push_back(divisor);
}
}
return divisors;
}
bool divisorGame(int N) {
vector<bool> win(N+1, false);
win[1] = false;
win[2] = true;
for(int i = 3; i <= N; i++){
vector<int> divisors = getDivisors(i);
// cout << "i: " << i << endl;
// copy(divisors.begin(), divisors.end(), ostream_iterator<int>(cout, " "));
// cout << endl;
// cout << "win: " << win[i] << endl;
for(int divisor : divisors){
if(!win[i - divisor]){
win[i] = true;
break;
}
}
// cout << "win: " << win[i] << endl;
}
return win[N];
}
};
//make all_divisors an array of array
//Runtime: 20 ms, faster than 6.95% of C++ online submissions for Divisor Game.
//Memory Usage: 10.6 MB, less than 6.90% of C++ online submissions for Divisor Game.
class Solution {
public:
vector<vector<int>> all_divisors;
vector<int> getDivisors(int x){
vector<int> divisors;
for(int divisor = x/2; divisor >= 1; divisor--){
if(x % divisor == 0){
if(divisor == 1){
divisors.push_back(divisor);
}else{
if(divisor == x / divisor){
divisors = all_divisors[divisor];
divisors.push_back(divisor);
}else{
vector<int> a = all_divisors[divisor];
vector<int> b = all_divisors[x / divisor];
sort(a.begin(), a.end());
sort(b.begin(), b.end());
divisors = vector<int>(a.size() + b.size());
std::vector<int>::iterator it = std::set_union(a.begin(), a.end(), b.begin(), b.end(), divisors.begin());
divisors.resize(it - divisors.begin());
divisors.push_back(divisor);
divisors.push_back(x / divisor);
}
}
break;
}
}
return divisors;
}
bool divisorGame(int N) {
vector<bool> win(N+1, false);
all_divisors = vector<vector<int>>(N+1);
win[1] = false;
for(int i = 2; i <= N; i++){
cout << "i: " << i << endl;
all_divisors[i] = getDivisors(i);
copy(all_divisors[i].begin(), all_divisors[i].end(), ostream_iterator<int>(cout, " "));
cout << endl;
// cout << "win: " << win[i] << endl;
//if it can find a divisor so that win[i-divisor] is false, i will win
for(int divisor : all_divisors[i]){
if(!win[i - divisor]){
win[i] = true;
break;
}
}
// cout << "win: " << win[i] << endl;
}
return win[N];
}
};
//https://leetcode.com/problems/divisor-game/discuss/274566/just-return-N-2-0-(proof)
//Runtime: 4 ms, faster than 61.78% of C++ online submissions for Divisor Game.
//Memory Usage: 8.1 MB, less than 96.55% of C++ online submissions for Divisor Game.
class Solution {
public:
bool divisorGame(int N) {
return N%2 == 0;
}
};
//Runtime: 4 ms, faster than 61.78% of C++ online submissions for Divisor Game.
//Memory Usage: 8.1 MB, less than 100.00% of C++ online submissions for Divisor Game.
class Solution {
public:
bool divisorGame(int N) {
return !(N & 1);
}
};