Open azl397985856 opened 5 months ago
class Solution:
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
import functools
if desiredTotal <= maxChoosableInteger: return True
if sum(range(maxChoosableInteger + 1)) < desiredTotal: return False
@functools.lru_cache(None)
def dfs(used, desiredTotal):
for i in range(maxChoosableInteger):
cur = 1 << i
if cur & used == 0:
if desiredTotal <= i + 1 or not dfs(cur | used, desiredTotal - i - 1):
return True
return False
return dfs(0, desiredTotal)
class Solution { public boolean canIWin(int maxChoosableInteger, int desiredTotal) { if (desiredTotal <= maxChoosableInteger || desiredTotal == 0) return true; if (desiredTotal > ((maxChoosableInteger * (maxChoosableInteger + 1)) / 2)) return false;
if (1 + maxChoosableInteger < desiredTotal && desiredTotal < 2 * maxChoosableInteger)
return true;
if (1 + maxChoosableInteger == desiredTotal && desiredTotal < 2 * maxChoosableInteger)
return false;
if (desiredTotal % 2 == 0 && maxChoosableInteger % 2 == 0) {
return desiredTotal / maxChoosableInteger < 4 || desiredTotal == 100 && maxChoosableInteger == 16;
} else {
return desiredTotal % 2 == 0 || maxChoosableInteger % 2 != 0 || desiredTotal / maxChoosableInteger < 5;
}
}
}
464. 我能赢么
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/can-i-win/
前置知识
暂无
题目描述