You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner.
Write a function to determine if the starting player can guarantee a win.
For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+".
Follow up:
Derive your algorithm's runtime complexity.
Hide Company Tags Google
Hide Tags Backtracking
Hide Similar Problems (E) Nim Game (E) Flip Game
这题用简单的backtracking就可以解决, 但time complexity 很高。所以看了一个很牛的theory用dp做(为什么差距这么大,哭哭。。。) https://leetcode.com/discuss/64344/theory-matters-from-backtracking-128ms-to-dp-0ms 据说面试不需要用这么牛x 的理论,只要backtracking就好啦~
class Solution {
public:
// back tracking 200ms:
bool canWin(string s) {
if(s.empty() || s.size() < 2) return false;
for(int i = 0; i<=s.size()-2; ++i){
if(s[i] == '+' && s[i+1] == '+'){
s[i] = s[i+1] = '-';
if(!canWin(s)) return true;
s[i] = s[i+1] = '+';
}
}
return false;
}
};
下面是0ms的dp:
class Solution {
public:
int firstMissingNumber(unordered_set<int> lut) {
int m = lut.size();
for (int i = 0; i < m; ++i) {
if (lut.count(i) == 0) return i;
}
return m;
}
bool canWin(string s) {
int curlen = 0, maxlen = 0;
vector<int> board_init_state;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '+') curlen++; // Find the length of all continuous '+' signs
if (i+1 == s.size() || s[i] == '-') {
if (curlen >= 2) board_init_state.push_back(curlen); // only length >= 2 counts
maxlen = max(maxlen, curlen); // Also get the maximum continuous length
curlen = 0;
}
} // For instance ++--+--++++-+ will be represented as (2, 4)
vector<int> g(maxlen+1, 0); // Sprague-Grundy function of 0 ~ maxlen
for (int len = 2; len <= maxlen; ++len) {
unordered_set<int> gsub; // the S-G value of all subgame states
for (int len_first_game = 0; len_first_game < len/2; ++len_first_game) {
int len_second_game = len - len_first_game - 2;
// Theorem 2: g[game] = g[subgame1]^g[subgame2]^g[subgame3]...;
gsub.insert(g[len_first_game] ^ g[len_second_game]);
}
g[len] = firstMissingNumber(gsub);
}
int g_final = 0;
for (auto& s: board_init_state) g_final ^= g[s];
return g_final != 0; // Theorem 1: First player must win iff g(current_state) != 0
}
};