Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
divisor-game.cpp 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-04-26 00:23 +08:00 . Update divisor-game.cpp
// Time: O(1)
// Space: O(1)
class Solution {
public:
bool divisorGame(int N) {
// 1. if we get an even, we can choose x = 1
// to make the opponent always get an odd
// 2. if the opponent gets an odd, he can only choose x = 1 or other odds
// and we can still get an even
// 3. at the end, the opponent can only choose x = 1 and we win
// 4. in summary, we win if only if we get an even and
// keeps even until the opponent loses
return N % 2 == 0;
}
};
// Time: O(n^3/2)
// Space: O(n)
// dp solution
class Solution2 {
public:
bool divisorGame(int N) {
unordered_map<int, int> dp;
return memoization(N, &dp);
}
private:
bool memoization(int N, unordered_map<int, int> *dp) {
if (N == 1) {
return false;
}
if (!dp->count(N)) {
bool result = false;
for (auto i = 1; i * i <= N; ++i) {
if (N % i == 0) {
if (!memoization(N - i, dp)) {
result = true;
break;
}
}
}
(*dp)[N] = result;
}
return (*dp)[N];
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助