Участник:Timatizak/can i win

Материал из DISCOPAL
Перейти к: навигация, поиск
import functools
class Solution:
    def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
        sum_of_all = (maxChoosableInteger + 1) * maxChoosableInteger // 2
        if sum_of_all < desiredTotal:
            return False
        if sum_of_all == desiredTotal:
            return maxChoosableInteger % 2 == 1
        @functools.lru_cache(None)
        def can_i_win(options, target):
            if options[-1] >= target:
                return True
            return not all(can_i_win(options[:i] + options[i + 1:],target - x) for i, x in enumerate(options))
 
        return can_i_win(tuple(range(1, maxChoosableInteger + 1)), desiredTotal)