294. 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 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的更多相关文章
- leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...
- [LeetCode] 294. Flip Game II 翻转游戏 II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode 294. Flip Game II
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
- 【LeetCode】294. Flip Game II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 日期 题目地址:https://leetc ...
- [LeetCode] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- LeetCode Flip Game II
原题链接在这里:https://leetcode.com/problems/flip-game-ii/ 题目: You are playing the following Flip Game with ...
- [Swift]LeetCode294. 翻转游戏之 II $ Flip Game II
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- Flip Game II -- LeetCode
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- 294. 翻转游戏 II
题目: 链接:https://leetcode-cn.com/problems/flip-game-ii/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...
随机推荐
- 设计模式 -- 单例模式(Java&&PHP)
所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就像是Java Web中的application,也就是提供了一个全局变量,用处相当广泛,比如保存全局数据,实现全局性的操作等. 能够 ...
- 团队开发NABC特点
一.软件特点 我们所设计的躲避小球游戏,能够防止用户沉迷,且不会使用户有挫败感,失去玩游戏的兴趣. 二.进行NABC分析 N:我们适应于用户对娱乐.打发时间需求 A:我们用图形界面实现 B:此游戏适用 ...
- Asp.Net 操作word 第二篇[推荐]
引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示 ...
- 【转】android ListView 几个重要属性
android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...
- 【CentOS】Eclipse插件egit使用
1.简介 2.安装 3.配置 4.使用 5.补充说明 参考资料: http://yufenfei.iteye.com/blog/1750124 1.简介 EGit就是一款Eclips ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- NVelocity 在.Net的三种用法
NVelocity 使用文件型模板例子 using NVelocity; using NVelocity.App; using NVelocity.Runtime; VelocityEngine vl ...
- UICollectionViewLayout
http://blog.csdn.net/majiakun1/article/details/17204921
- SOA之(1)——SOA架构基础概念
在深入探讨什么是面向服务的架构(SOA)之前,先建立一些基本的概念和术语的基本描述而非严格定义,所以也许有些定义在业内还存留争议,此处暂且忽略. 架构基础 技术架构(Technology Archit ...
- 从maya导入物体 Importing Objects From Maya
原地址:http://game.ceeger.com/Manual/HOWTO-ImportObjectMaya.html Unity natively imports Maya files. To ...