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/ 你和朋友玩一个叫做「翻转游戏」的游戏,游戏规则:给定一个只有 + 和 - 的字符串.你和朋友 ...
随机推荐
- 随机的30道四则运算题(简单的c)
#include <stdio.h>#include <stdlib.h>#include <time.h> int main(void){ int i = 0; ...
- dd面试经历
HR面:看了我的简历,说fe做的简历就是不一样哈哈好吧,然后随便问了点项目,又问了什么时候可以去实习,就没了.三面:基本数据结构.冒泡排序.数组去重.ie与主流浏览器事件绑定.垂直居中的css实现方 ...
- 关于Google+以及Facebook第三方登录实现的一点总结
简述 最近项目中有关于第三方登陆的需求,第三方Facebook以及Google +登录. 正好这几天把这个需求做得差不多了,收个尾,作为一个这方面之前基本从未涉及的小白,总结下开发流程以及过程中遇到的 ...
- gvim编辑文件到github乱码
with below _vimrc settings, code uploaded to GitHub will display with proper encoding set encoding=u ...
- C++ 11 新特性
C++11新特性: 1.auto 2.nullptr 3.for 4.lambda表达式 5.override ...
- 推荐系统之LFM
这里我想给大家介绍另外一种推荐系统,这种算法叫做潜在因子(Latent Factor)算法.这种算法是在NetFlix(没错,就是用大数据捧火<纸牌屋>的那家公司)的推荐算法竞赛中获奖的算 ...
- 创建REST服务应用程序
Web服务类别有两种,一种是基于SOAP协议的服务,另一种是基于HTTP协议的REST架构风格的服务.REST服务的数据格式有两种:XML 和 JSON,REST服务已被大量应用于移动互联网中. 本文 ...
- 【bzoj1013】[JSOI2008]球形空间产生器sphere
1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4530 Solved: 2364[Subm ...
- HDOJ 1062 Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hadoop安装问题
1. 运行start-dfs.sh启动HDFS守护进程,start-yarn.sh面向YARN的资源器和节点管理器,资源管理器web地址是http://localhost:8080/.输入stop.d ...