[LeetCode294]Flip Game II

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
     }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值