楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.30-7.6 CareerCup 3.4

🔗
donnice 2014-7-5 06:29:02 | 只看该作者
全局:
【解题思路】
经典算法了不再赘述,具体请百度“汉诺塔”
【时间复杂度】
O(2^n)(我很想知道大家的O(N)是怎么出来的)
【空间复杂度】
O(N)
【gist】
https://github.com/donnice/donni ... 455e6761ba7c6007cf3
回复

使用道具 举报

🔗
Neal 2014-7-5 14:06:52 | 只看该作者
全局:
【解题思路】Recursion
【时间复杂度】O(2^n)
【空间复杂度】O(N)
【gist】https://gist.github.com/nealhu/f8d39eba344cfe8bda81
回复

使用道具 举报

🔗
pud 2014-7-6 02:04:47 | 只看该作者
全局:
【解题思路】Recursion
【时间复杂度】O(2^n)
【空间复杂度】O(N)
【gist】https://gist.github.com/yokiy/ceee7892798ca326a0e7

点评

有时间可以再尝试一下用非递归的方法实现:)  发表于 2014-7-6 14:22
回复

使用道具 举报

全局:
【解题思路】C.f. wikipedia: Tower of Hanoi
1. Recursive
2. Iterative: Alternate moves between the smallest piece and a non-smallest piece. When moving the smallest piece, always move it to the next position in the same direction (to the right if the starting number of pieces is even, to the left if the starting number of pieces is odd).
3. Simple iterative: for an even number of disks repeat legal moves between AB, AC and BC; for an odd number of disks repeat legal moves between AC, AB and BC, until the first two stacks are all empty. (OR until a total of 2^n-1 moves are made.)
【时间复杂度】
O(2^N) // minimum 2^n-1 moves
【空间复杂度】
O(N) if count on the via stack
【gist link】
https://gist.github.com/d945badeffb473c1fd12
回复

使用道具 举报

🔗
chouclee 2014-7-6 21:46:55 | 只看该作者
全局:
【解题思路】
用stack模拟递归, 参考http://hawstein.com/posts/3.4.html
【时间复杂度】
O(2^n) (具体为2^n - 1)
【空间复杂度】
O(n)  (具体为2n-1)
【gist link】
https://gist.github.com/chouclee/97dbc195b1c92b7b1418
回复

使用道具 举报

🔗
wilbert 2014-7-6 23:54:26 | 只看该作者
全局:
【解题思路】
recursive:
1. move n-1 disks to the Temp stack
2. move the last disk from From stack to To stack
3. move n-1 disks from Temp stack to To stack
【时间复杂度】
O(2^N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/iwilbert/9ab3ff91d4730f3b1dd0
回复

使用道具 举报

🔗
林微熙 2014-7-7 05:05:38 | 只看该作者
全局:
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】

这两天补
回复

使用道具 举报

🔗
jason51122 2014-7-7 13:33:21 | 只看该作者
全局:
【解题思路】An recursion problem. Move n-1 from tower 0 to tower 1. Then move 1 from tower 0 to tower 2. Move n-1 from tower 1 to tower 2.
【时间复杂度】O(2^N)
【空间复杂度】O(N)
【gist link】https://gist.github.com/jason51122/009c4fa7f4166bc59906
回复

使用道具 举报

🔗
jason51122 2014-7-7 13:41:11 | 只看该作者
全局:
本帖最后由 jason51122 于 2014-7-7 13:42 编辑
chouclee 发表于 2014-7-6 21:46
【解题思路】
用stack模拟递归, 参考http://hawstein.com/posts/3.4.html
【时间复杂度】

Why the space complexity is 2n-1? We have n disks in total and move them within 3 towers. The maximum # of disks is n-1 for tower 2 and n for tower 0 and tower 2. So I think the space complexity is 3n-1, which is O(n). Thanks.
回复

使用道具 举报

🔗
锦木千束 2014-7-7 13:57:39 | 只看该作者
全局:
【解题思路】
an classic recursion problem
for n-order Hanoi Tower:
1. move n - 1 disks from tower A to tower B
2. move the biggest disk N from A to C
3. move n - 1 disks from B to C
if n==1 move the only disk from A to C directly
【时间复杂度】O(2^n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/weazord/2b605eb3bf24e9468467
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表