Divisor Game Solutions in C++
Number 1025
Difficulty Easy
Acceptance 66.3%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/divisor-game//// Author : liuyubobobo/// Time : 2019-04-13#include <iostream>#include <vector>using namespace std;/// Dynamic Programming/// Time Complexity: O(n^2)/// Space Complexity: O(n)class Solution {public:bool divisorGame(int N) {vector<int> dp(N + 1, -1);return dfs(N, dp, true);}private:bool dfs(int x, vector<int>& dp, bool isAlice){if(x == 1) return isAlice ? false : true;if(dp[x] != -1) return dp[x];if(isAlice){for(int i = 1; i * i <= x; i ++)if(x % i == 0 && dfs(x - i, dp, false))return dp[x] = true;return dp[x] = false;}else{for(int i = 1; i * i <= x; i ++)if(x % i == 0 && !dfs(x - i, dp, true))return dp[x] = false;return dp[x] = true;}}};int main() {cout << Solution().divisorGame(2) << endl; // truecout << Solution().divisorGame(3) << endl; // falsecout << Solution().divisorGame(9) << endl; // falsereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/divisor-game//// Author : liuyubobobo/// Time : 2019-04-13#include <iostream>#include <vector>using namespace std;/// Mathematics/// Prove can be see here: https://leetcode.com/problems/divisor-game/discuss/274606/JavaC%2B%2BPython-return-N-2-0////// Time Complexity: O(1)/// Space Complexity: O(1)class Solution {public:bool divisorGame(int N) {return N % 2 == 0;}};int main() {cout << Solution().divisorGame(2) << endl; // truecout << Solution().divisorGame(3) << endl; // falsecout << Solution().divisorGame(9) << endl; // falsereturn 0;}