294. Flip Game II

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;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,493评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,451评论 3 20
  • 365计划的第6天 阿宝放下手机,机身热热的,因为阿宝用它连着看了两集《太阳的后裔》。 此刻,阿宝的脑袋也热热的,...
    临窗画小山阅读 434评论 2 3
  • 前一段时间早上醒来刷微博,看到了某地一醉酒司机撞倒环卫工人(一死两伤)的新闻,顺便看了下评论,我认为评论中只有...
    空白的温暖阅读 249评论 0 1
  • 我喜欢讲生活中的小故事,因为我很容易会被很小的事情所感动。 最近的日子,发现自己实在很难去违背自己的想法去做任何的...
    LLLiberty阅读 236评论 0 0