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

[CareerCup] CareerCup 12.3

全局:

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

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

x
Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB of memory.
FOLLOW UP
What if you have only 10MB of memory?


上一篇:CareerCup 12.2
下一篇:CareerCup 12.4
🔗
toliuweijing 2012-8-31 23:54:30 | 只看该作者
全局:
Integers have range from -2^31 to 2^31 and so they can be mapped into a 2^32 bits map.
And with 1GB we can have a 2^38 bits map. It can be done in 2 pass processing So O(n).

I didn't finish the followup though and just found a solution from some other websites.

The idea is that a sequence of bits can do not only mapping but also counting. It only needs 32 bits for counting upto 2^32. That is the power of permutation.
So we can divide those integers into blocks of 1024 and set a counter for each block. For example, 515 is between 1~1000, so counter[0]++.
Doing like this, we need 2^32/2^10= 2^22 blocks. and 10 bits for per block. Totally it needs 2^22*10 < 10MB.
After counting thru the integers, we look for any range whose counter is less than 1000.
Now it's sure that there's a gap in the range and it's trivial to get it with 10MB memory.

The only doubt I have is what if all counters are over 1000. I guess it's not gonna happen because 4 billion < 2^32.
回复

使用道具 举报

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

本版积分规则

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