最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性。该题的描述翻译成中文如下:

你正在和你的朋友玩尼姆游戏(Nim Game): 桌子上有一堆石块,你和你的朋友轮流去拿这些石块,每次只能拿1块、2块或者3块。在石块被拿光前,最后一次拿到石块的人获胜。你将首先去拿这些石块。 你和你的朋友都非常聪明,并且拥有应对该游戏的最佳策略。写一个函数来决定在给定石块数量的情况下,你是否能够获胜。比如:如果桌子上有4块石块,那么你将不可能获胜:不管你先拿出1块、2块还是3块石头,最后一次拿光石块的永远都会是你的朋友。(There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.)

拿到题目后,习惯性地点开Hint看了一下:

Hint:

  1. 如果桌子上有5块石头,你能找到确保你会获胜的拿石块的方法吗?(If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?)

第一反应自然是递归了,于是写下了下面的code:

  public static boolean canWinNim_recurion(int stoneCount) {
if(stoneCount<=0) {
System.out.println("illegal input");
return false;
}
/* you will win */
if((0<stoneCount)&&(stoneCount<4)) {
return true;
} return !(canWinNim_recurion(stoneCount-1)&&canWinNim_recurion(stoneCount-2)&&canWinNim_recurion(stoneCount-3));
}

提交后给出StackOverflowError,自己在本机上用大数(1348820612)试验了一下也是StackOverflowError。原因自然是递归太深了。于是又写下了下面的code:

  public static boolean canWinNim(int stoneCount) {
if(stoneCount<=0) {
System.out.println("illegal input");
return false;
} boolean[] canWinArray = new boolean[stoneCount];
for(int i=1;i<=stoneCount;i++) {
if(i==1||i==2||i==3) {
canWinArray[i-1] = true;
continue;
}
canWinArray[i-1] = !(canWinArray[i-2]&&canWinArray[i-3]&&canWinArray[i-4]);
}
return canWinArray[stoneCount-1];
}

这一次的思路是把递归改成循环,而且为了简单起见,是自底向上的循环。没记错的话这其实是一个DP的解法。但是提交后,给出Memory Limit Exceeded错误,那就是O(n)的空间复杂度不符合要求了。于是给出了下面的空间复杂度为常量的code:

  public boolean canWinNim2(int stoneCount) {
boolean canWin = false; if(stoneCount<=0) {
System.out.println("illegal input");
return false;
} /* only need a boolean array of length 3 ? */
//boolean[] canWinArray = new boolean[stoneCount];
boolean[] last3CanWinArray = new boolean[3]; for(int i=1;i<=stoneCount;i++) {
if(i==1||i==2||i==3) {
last3CanWinArray[i-1] = true;
canWin = true;
continue;
}
canWin = !(last3CanWinArray[0]&&last3CanWinArray[1]&&last3CanWinArray[2]);
// update the array
last3CanWinArray[0] = last3CanWinArray[1]; // the index cannot be i-2 i-3 etc.
last3CanWinArray[1] = last3CanWinArray[2];
last3CanWinArray[2] = canWin;
}
return canWin;
}

但是提交后提示Time Limit Exceeded。这个算法的时间复杂度为O(n),难道还不符合要求吗? 百思不得其解,google了一下,发现这道题的考察意图在找规律而不在写code。然后终于找到了规律:被4整除的石头数,都不可能赢;其他数则能赢。那么代码就太简单了,一行就搞定:

  public boolean canWinNim_brainteaser(int stoneCount) {
return stoneCount%4!=0;
}

提交后提示成功,至此算是把这题刷完了。

失败总结:其实,在这道题的描述和Hint后头,有一个tag标签,点开可以看到清清楚楚地写着:brainteaser。也就是脑筋急转弯。而我还老是往DP方面去想,结果自然是悲剧了。刷这道题给了我一个教训:就像高中时刷题一样,对待算法题一定要注意审题,不漏掉题目给出的每一个细节,重视每一个给出的提示,并按照提示给出的方向去思考。

一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)的更多相关文章

  1. 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)(转)

    最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...

  2. LeetCode 刷题 App / LeetCode 题解 App

    LeetCode 刷题 APP / LeetCode 题解 App 全端支持 http://leetcode-app.xgqfrms.xyz/ http://leetcode-desktop.xgqf ...

  3. 【刷题】BZOJ 1413 [ZJOI2009]取石子游戏

    Description 在研究过Nim游戏及各种变种之后,Orez又发现了一种全新的取石子游戏,这个游戏是这样的: 有n堆石子,将这n堆石子摆成一排.游戏由两个人进行,两人轮流操作,每次操作者都可以从 ...

  4. [LeetCode] Nim Game 尼姆游戏

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  5. leetCode之旅(5)-博弈论中极为经典的尼姆游戏

    题目介绍 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...

  6. C#LeetCode刷题-脑筋急转弯

    脑筋急转弯篇 # 题名 刷题 通过率 难度 292 Nim游戏   62.5% 简单 319 灯泡开关   31.8% 中等 777 在LR字符串中交换相邻字符   21.9% 中等

  7. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. 在vscode中配置LeetCode插件,从此愉快地刷题

    大家好,今早在B站看到up主的vscode里藏了leetcode插件,这才知道原来还有这款神器.但是没想到在用的时候遇到了一些麻烦,花了一点时间才解决.所以写这篇文章除了给大家安利这个好用的插件之外, ...

随机推荐

  1. ArcGIS for Android 中实现要素绘制时固定MapView

    最近在项目中遇到这么一个情况,在MapView中要求实现绘制点.线.面. 在这里面就会遇到这么一个问题,绘制折线和多边形型时,每点击一个点屏幕就会跟着晃,使用起来很不方便(使用Note2 触控笔),所 ...

  2. Python学习-用户输入和字符串拼接

      用户输入和字符串拼接 #用户输入和字符串拼接username=input("username:")age=int(input("Age:")) #转换整数型 ...

  3. Winform调整DEV控件高度

  4. PHP代码规范的一些总结

    世界第一语言在手,辅以前人的最佳实践,天下又算什么. 1.代码是写给小白用的 注释,注释,注释,重要的事情说三遍.我们做的虽然不是拿去卖源码的商业产品,不需要把注释写的多么优美.但也不要太过吝啬,到头 ...

  5. RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍

    1. AMF AMF(是Action Message Format的缩写)是在flash和flex中与远程服务端交换数据的一种格式.它是二进制格式,Flash应用与服务端或数据库通过RPC交换数据时, ...

  6. Codeforces 744A. Hongcow Builds A Nation

    A. Hongcow Builds A Nation 题意: 现在有 n 个点 ,m 条边组成了一个无向图 , 其中有 k 个特殊点, 这些特殊点之间不能连通 ,问可以再多加几条边? 因为$x^2+y ...

  7. IOS 线程描述

    ●什么是线程 ● 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) ● 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 ● 比如使用酷狗播放音乐.使用迅雷下载电影, ...

  8. 使ListView控件中的选择项高亮显示

    实现效果: 知识运用: ListView控件的SelectedItems属性 //获取在ListView控件中被选中数据项的集合 public ListView.SelectedListViewIte ...

  9. unsigned __int64 打印方法

    原文出处 long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647. 而unsigned范围是[0,2^32),即0~4294967295.也就是说,常规的 ...

  10. Access数据库远程连接的实用方法

    一般在远程文件夹开启文件共享即可通过像平常一样用连接字符串访问,注意共享的读写权限. 远程(如通过互联网)连接access数据库的示例: 首先,需要使用TCP/IP,ADO及XML(需要安装Micro ...