查看: 2203| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 5/14-5/20 CareerCup 5.7

全局:

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

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

x
An array A[1...n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is "fetch the jth bit of A[i]", which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?

上一篇:5/14-5/20 CareerCup 5.6
下一篇:5/14-5/20 CareerCup 6.1
🔗
writecoffee1 2012-6-13 23:54:09 | 只看该作者
全局:
本帖最后由 writecoffee1 于 2012-6-14 00:16 编辑

https://github.com/writecoffee/career_cup_iteration/blob/improved/5.7.cpp


//////////////////////////////////////////////////////////////////////////////////////
// GIST
// 1.        since the array starts from zero, in the LSB, originally bit-0 amount should
//        be at least equivalent to that of bit-1
// 2.        based on the above statement, every time 'imbalence' occurs (i.e. when one of
//        the 0-bits was removed, bit-1 amount would be bigger or equals to that of the
//        bit-0; on the opposite, when one of the 1-bits was removed, bit-0 amount just
//        naturally bigger than that of the bit-1) we can get the missing bit and
//        eliminate half the array elements where the opposite bit exists
// 3.         recurse step-2 from LSB to MSB repeatedly
// (4.)        when array becomes empty, fill up 0-bit in the front of the binary result
//
// TIME_COMPLEXITY
//         at each level, we need (N-1) / 2^lev operations, 'width' levels combined is
//        2N, namely O(N)
////////////////////////////////////////////////////////////////////////////////////
回复

使用道具 举报

🔗
xuyirio 2012-7-22 00:28:18 | 只看该作者
全局:
回复

使用道具 举报

🔗
xusi 2012-9-22 06:06:07 | 只看该作者
全局:
感觉这个应该比答案好
https://github.com/0x7ffff/Career-Cup-archive/blob/master/5/7.c

比如n=5, miss=3, 那把数组里的元素和[0-5]通通xor一遍,结果就是3了
0^1^2^3^4^5^5^4^1^2 = 3
回复

使用道具 举报

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

本版积分规则

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