📣 独立日限时特惠: VIP通行证立减$68
查看: 2918| 回复: 19
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】7.7-7.13 CareerCup 4.3

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
4.3 Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。



上一篇:【第三轮】7.7-7.13 CareerCup 4.2
下一篇:【第三轮】7.7-7.13 CareerCup 4.4
推荐
grassgigi 2014-7-7 01:02:12 | 只看该作者
全局:
【解题思路】
Recursively created bst whose root is the mid point of lo and hi elements in array

【时间复杂度】
since O(N) = 2 * O(N/2) + O(1)
=> O(lgN) - N is # of elements in array

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/chrislukkk/be47c3281a3911c10475
BST: https://gist.github.com/chrislukkk/2a1f825b8e924ea773e8

【test case】

点评

时间复杂度算错了,应该是O(n)  发表于 2014-7-11 23:12

评分

参与人数 1大米 +3 收起 理由
donnice + 3 这个递归很漂亮

查看全部评分

回复

使用道具 举报

推荐
chouclee 2014-7-11 20:28:06 | 只看该作者
全局:
【解题思路】
recursion. 将array [low,...,mid-1, mid, mid+1,... high]分成3个部分[low,...,mid-1], [mid] 和[mid+1,...,high],对于当前node,放入[mid], 然后将[low,...,mid-1]的中位数放入node.left,将[mid+1,...,high]的中位数放入node.right
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/chouclee/683394edb16d86949ce8
回复

使用道具 举报

🔗
ryancooper 2014-7-9 02:15:50 | 只看该作者
全局:
grassgigi 发表于 2014-7-7 01:02
【解题思路】
Recursively created bst whose root is the mid point of lo and hi elements in array

Your time complexity is not correct, it should be O(n). it can be deducted as follow:
lg(T(N)) = 1 + lg(T(N/2)) => lg(T(N)) = lgN => T(N) = N
回复

使用道具 举报

🔗
donnice 2014-7-10 01:52:41 | 只看该作者
全局:
【解题思路】优先取出中间值作为root,取出左半边的中间值作为root.left, 右半边为root.right,递归
程序参考了沙发的写法,非常漂亮的递归
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist】
https://github.com/donnice/donnice/blob/master/Q4_3
回复

使用道具 举报

🔗
bitcpf 2014-7-11 00:01:20 | 只看该作者
全局:
【解题思路】recursion, ref to the 2nd
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist】https://gist.github.com/bitcpf/7c5d19d378c7a595ef57
回复

使用道具 举报

🔗
Neal 2014-7-11 08:34:15 | 只看该作者
全局:
【解题思路】divid and conquer.
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist】https://gist.github.com/nealhu/5580fa4178d2e81528df
回复

使用道具 举报

🔗
jason51122 2014-7-11 13:07:05 | 只看该作者
全局:
【解题思路】Binary search.
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】https://gist.github.com/jason51122/471a22c93c3f65a663c6
回复

使用道具 举报

全局:
【解题思路】
recursion


【时间复杂度】
O(n)

【空间复杂度】
O(logn)

【gist link】
https://gist.github.com/happyWinner/cffc5643c6e013b7470f




评分

参与人数 1大米 +2 收起 理由
kimiflasky + 2 感谢分享!

查看全部评分

回复

使用道具 举报

全局:
为什么楼上的你们的空间复杂度都是O(n)?不应该是O(log n)么
回复

使用道具 举报

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

本版积分规则

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