can I win(judge first player to move can win)
来源:互联网 发布:php常用函数有哪几个 编辑:程序博客网 时间:2024/06/14 11:22
/* In "the 100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins. What if we change the game so that players cannot re-use integers? For example, if two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100. This problem is to write a program that determines which player would win with ideal play. Write a procedure, "Boolean canIWin(int maxChoosableInteger, int desiredTotal)", which returns true if the first player to move can force a win with optimal play. Your priority should be programmer efficiency; don't focus on minimizing either space or time complexity. */ Boolean canIWin(int maxChoosableInteger, int desiredTotal) { // Implementation here. Write yours }
Solution from here http://codeanytime.blogspot.com/2015/01/caniwin.html
//The catch is: when the largest number remaining is greater than the target remaining, the player is sure to win//helper returns true if the CURRENT player can winpublic boolean canIWin(int max, int target) { List<Integer> candidates = new ArrayList<Integer>(); for (int i = 1; i <= max; i++){ candidates.add(i); } return helper(candidates, target);}public boolean helper(List<Integer> candidates, int target){ if (candidates.get(candidates.size()-1) >= target){ return true; } for (int i = 0; i < candidates.size(); i++){ int removed = candidates.remove(i); if (!helper(candidates, target-removed)){ candidates.add(i, removed); return true; } candidates.add(i, removed); } return false;}
0 0
- can I win(judge first player to move can win)
- 464. Can I Win
- [LeetCode]Can I Win
- Can I Win
- 464. Can I Win
- 464. Can I Win**
- 464. Can I Win
- 464. Can I Win
- LeetCode13 Can I win
- [LeetCode] Can I Win
- 464. Can I Win
- 464. Can I Win
- [leetcode 464]Can I Win
- Leetcode-464. Can I Win
- LeetCode 464. Can I Win
- 464. Can I Win -Medium
- [leetcode]464. Can I Win
- LeetCode 464. Can I Win
- ios 预留
- reverse a linked list
- poj 2352 树状数组
- 把web项目改造为maven项目
- java Log4j使用介绍
- can I win(judge first player to move can win)
- 杭电 HDU ACM 1708 Fibonacci String
- 2015.4.7-4.8 widget中使用Listview (上)分析说明
- 打造自己的安卓Metro界面
- OpenCV之利用鼠标获取坐标
- 黑马程序员-JAVASE入门(内省、注解、类加载器)
- 3-way TCP connection establishment
- Perfect hashing (And Minimal perfect hashing)
- 文章标题