题目大意:两个人从1到M中每次取出一个数加到当前的总和上,第一个达到或超过T的人获胜。问你第一个玩家能不能获胜。
In the “100 game,” two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.
Given an integer maxChoosableInteger
and another integer desiredTotal
, determine if the first player to move can force a win, assuming both players play optimally.
You can always assume that maxChoosableInteger
will not be larger than 20 and desiredTotal
will not be larger than 300.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Input: maxChoosableInteger = 10 desiredTotal = 11 Output: false Explanation: No matter which integer the first player choose, the first player will lose. The first player can choose an integer from 1 up to 10. If the first player choose 1, the second player can only choose integers from 2 up to 10. The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal. Same with other integers chosen by the first player, the second player will always win. |
Solution: Recursion with memoization
Time complexity: O(2^M)
Space complexity: O(2^M)
C++
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 |
// Author: Huahua // Running time: 19 ms (<98.70%) class Solution { public: bool canIWin(int M, int T) { const int sum = M * (M + 1) / 2; if (sum < T) return false; if (T <= 0) return true; m_ = vector<char>(1 << M, 0); return canIWin(M, T, 0); } private: vector<char> m_; // 0: unknown, 1: won, -1: lost bool canIWin(int M, int T, int state) { if (T <= 0) return false; if (m_[state]) return m_[state] == 1; for (int i = 0; i < M; ++i) { if (state & (1 << i)) continue; // number i used // The next player can not win, current player wins if (!canIWin(M, T - (i + 1), state | (1 << i))) return m_[state] = 1; } // Current player loses. m_[state] = -1; return false; } }; |
Java
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 |
// Author: Huahua // Running time: 20 ms class Solution { private byte[] m_; public boolean canIWin(int M, int T) { int sum = M * (M + 1) / 2; if (sum < T) return false; if (T <= 0) return true; m_ = new byte[1 << M]; return canIWin(M, T, 0); } private boolean canIWin(int M, int T, int state) { if (T <= 0) return false; if (m_[state] != 0) return m_[state] == 1; for (int i = 0; i < M; ++i) { if ((state & (1 << i)) > 0) continue; if (!canIWin(M, T - (i + 1), state | (1 << i))) { m_[state] = 1; return true; } } m_[state] = -1; return false; } } |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
""" Author: Huahua Running time: 706 ms """ class Solution: def canIWin(self, M, T): def win(M, T, m, state): if T <= 0: return False if m[state] != 0: return m[state] == 1 for i in range(M): if (state & (1 << i)) > 0: continue if not win(M, T - i - 1, m, state | (1 << i)): m[state] = 1 return True m[state] = -1 return False s = M * (M + 1) / 2 if s < T: return False if T <= 0: return True if s == T: return (M % 2) == 1 m = [0] * (1 << M) return win(M, T, m, 0) |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment