题目:

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 twoconsecutive "++" 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.

链接:  http://leetcode.com/problems/flip-game-ii/

题解:

求是否startng player可以有一种策略保证赢取游戏。直觉就是dfs +backtracking。 代码和Flip Game I基本一样,不过加入了验证下一步的一个条件语句。假如下一步next,对手不能赢,则这一步我们可以赢。看了Discuss以后发现还可以有O(n2)的DP做法,有些Game Theory的成分。Stellari大神好厉害。

Time Complexity - O(2n), Space Complexity - O(2n)

public class Solution {
public boolean canWin(String s) {
char[] arr = s.toCharArray();
for(int i = 1; i < s.length(); i++) {
if(arr[i] == '+' && arr[i - 1] == '+') {
arr[i] = '-';
arr[i - 1] = '-';
String next = String.valueOf(arr);
if(!canWin(next)) {
return true;
}
arr[i] = '+';
arr[i - 1] = '+';
}
} return false;
}
}

Reference:

https://leetcode.com/discuss/64344/theory-matters-from-backtracking-128ms-to-dp-0ms

https://leetcode.com/discuss/64291/share-my-java-backtracking-solution

https://leetcode.com/discuss/64522/simple-backtracking-inspired-by-flip-game-i

https://leetcode.com/discuss/64357/memoization-3150ms-130ms-44ms-python

https://leetcode.com/discuss/64486/backtracking-solution-time-optimization-through-205ms-19ms

https://leetcode.com/discuss/64350/short-java-%26-ruby

https://leetcode.com/discuss/64293/1-line-python-solution

https://leetcode.com/discuss/64332/java-recursive-backtracking-solution-27ms

https://leetcode.com/discuss/64302/easy-to-understand-java-solution

http://lucida.me/blog/developer-reading-list/

294. Flip Game II的更多相关文章

  1. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  2. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  3. LeetCode 294. Flip Game II

    原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...

  4. 【LeetCode】294. Flip Game II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 日期 题目地址:https://leetc ...

  5. [LeetCode] Flip Game II 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  6. LeetCode Flip Game II

    原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...

  7. [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  8. Flip Game II -- LeetCode

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  9. 294. 翻转游戏 II

    题目: 链接:https://leetcode-cn.com/problems/flip-game-ii/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...

随机推荐

  1. 微软职位内部推荐-Senior Software Engineer-Office Incubation

    微软近期Open的职位: Office China team is looking for experienced engineers to improve consumer experience i ...

  2. postmortem report of period M1

    一.设想和目标 1.我们的软件主要要解决学长设计的学霸系统中视频及文档的浏览功能问题. 2.时间相对充裕.不过对于我们这些零基础的人来说还是比较困难. 3.我们团队中不同意见通常会进行进一步讨论,说出 ...

  3. JBOSS和WebLogic区别

    JBoss: 1.  JBoss开放源代码Java EE实现,成本低,好控制. 2.  JBoss需要的内存和硬盘空间比较小,但是只适合做小项目. 3.  安装非常简单.先解压缩JBoss打包文件再配 ...

  4. [转载]解决zabbix在configure时候遇到的问题(Ubuntu)

    http://hi.baidu.com/ryq1xiuluo/item/6d37e658f1b90b13db16351d ./configure --enable-server --enable-ag ...

  5. Node.js 学习(三) NPM 使用介绍

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  6. 【Implement strStr() 】cpp

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  7. 好书推荐——《Soft Skill》

    这本书不是一本简单的叙述程序员职业规划和如何提高能力的书. 他论述了如何做一个高产,快乐,幸福的程序员,包括职业生涯,理财,学习,健身,信仰等各个方面的内容. 推荐给每一位伟大的拯救宇宙的程序员! 书 ...

  8. poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流

    题目链接:http://poj.org/problem?id=2711 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066 Your p ...

  9. 【BZOJ】【1087】【SCOI2005】互不侵犯King

    状压DP 我写的太水了……64ms才过,估计还有更好的做法,希望各位神犇不吝赐教>_<. 嗯这题很明显每一行都可以用一个2进制数表示放置方式的,(1表示放,0表示不放).然后预处理一下所有 ...

  10. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...