美文网首页
294. Flip Game II

294. Flip Game II

作者: Nancyberry | 来源:发表于2018-05-31 10:37 被阅读0次

Description

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.

Example:

Input: s = "++++"
Output: true
Explanation: The starting player can guarantee a win by flipping the middle "++" to become "+--+".

Follow up:
Derive your algorithm's runtime complexity.

Solution

Backtracking, O(n!), S(n)

时间复杂度有人说是O(n!!)?待研究。

The idea is try to replace every "++" in the current string s to "--" and see if the opponent can win or not, if the opponent cannot win, great, we win!

class Solution {
    public boolean canWin(String s) {
        if (s == null || s.length() < 2) {
            return false;
        }
        
        return canWin(s.toCharArray());
    }
    
    private boolean canWin(char[] arr) {
        // try to find a solution making nextCanNotWin
        boolean nextCanWin = true;
        
        for (int i = 0; i < arr.length - 1 && nextCanWin; ++i) {
            if (arr[i] == '+' && arr[i + 1] == '+') {
                arr[i] = '-';
                arr[i + 1] = '-';
                nextCanWin = canWin(arr);
                arr[i] = '+'; // don't forget to revert, even if nextCanNotWin!
                arr[i + 1] = '+';
            }
        }
        return !nextCanWin;
    }
}

Backtracking with memo, O(?), S(?)

class Solution {
    public boolean canWin(String s) {
        if (s == null || s.length() < 2) {
            return false;
        }
        
        return canWin(s.toCharArray(), new HashMap<>());
    }
    
    private boolean canWin(char[] arr, Map<String, Boolean> memo) {
        if (memo.containsKey(new String(arr))) {
            return memo.get(new String(arr));
        }
        
        // try to find a solution making nextCanNotWin
        boolean nextCanWin = true;
        
        for (int i = 0; i < arr.length - 1 && nextCanWin; ++i) {
            if (arr[i] == '+' && arr[i + 1] == '+') {
                arr[i] = '-';
                arr[i + 1] = '-';
                nextCanWin = canWin(arr, memo);
                arr[i] = '+'; // don't forget to revert, even if nextCanNotWin!
                arr[i + 1] = '+';
            }
        }
        
        boolean canWin = !nextCanWin;
        memo.put(new String(arr), canWin);
        return canWin;
    }
}

相关文章

  • 294. Flip Game II

    Description You are playing the following Flip Game with ...

  • 294. Flip Game II

    You are playing the following Flip Game with your friend:...

  • 294. Flip Game II

    You are playing the following Flip Game with your friend:...

  • Flip Game II

    https://www.lintcode.com/problem/flip-game-ii/description

  • Flip Game II

    题目You are playing the following Flip Game with your frien...

  • CUC-SUMMER-2-J

    J - Flip Game POJ - 1753 Flip game is played on a rectang...

  • LeetCode[15] - Flip Game II

    这个题目李特是屌炸天的。我飞了九牛二虎之力(路子对),但是代码写的七荤八素,好长好长好长好长的。结果正解,三四行就...

  • LintCode

    1042 Toeplitz Matrix Flip Game

  • 293. Flip Game

    Description You are playing the following Flip Game with ...

  • Flip Game

    题目You are playing the following Flip Game with your frien...

网友评论

      本文标题:294. Flip Game II

      本文链接:https://www.haomeiwen.com/subject/npafsftx.html