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

一道Codility上的面试题

🔗
zenw | 只看该作者 |倒序浏览
全局:

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

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

x

A positive integer N is given. Write a function:
           function solution(N);
that returns the sum of the values of the digits in a decimal representation of N! (fractorial of N).
For example, give N = 14, the function should return 45, because N! = 87178291200 and the sum of the values of its digits equals:
8+7+1+7+8+2+9+1+2+0+0 = 45
The function should return -1 if the result exceeds 100,000,000.
Assume that:
  • N is an integer within the range [1...2,000]


Complexity:
  • expected worst-case time complexity is O(N^2)
  • expected worst-case space complexity is O(N)


上一篇:超清,编程,面试,近百本电子书,共享(CC150抛砖开楼,后续新增,求加精,求大米)
下一篇:LeetCode好坑
推荐
xutopia 2014-12-24 02:49:26 | 只看该作者
全局:
本帖最后由 xutopia 于 2014-12-24 03:00 编辑

Assuming the space complexity asked is O(N) word complexity (i.e. O(N) 32-bit integers or something), and arithmetic of words is O(1) time.

1. Precompute everything, hardcode them in a table, do a table look up. O(1) time, if 100,000,000 fits in one word this is O(N) space.

2. Use linked list to implement large number multiplication in a big enough base, say base N. As N!<N^N, which in base-N representation only needs N numbers, so space complexity is O(N) words. Computing N! is O(N^2) time (actually no, see edit note below), once you have N! in base-N, computing the digits in base-10 should be easy.

Edit: In the 2nd approach computing N! in base N should be O(N^2 log N) using FFT, not sure how to get it down to O(N^2) for now, maybe do (N / log N) based arithmetic instead.
回复

使用道具 举报

🔗
nemoczech 2014-12-22 01:10:21 | 只看该作者
全局:
LZ能不能share下这题的题目名称是什么?谢谢了~
回复

使用道具 举报

🔗
 楼主| zenw 2014-12-22 01:11:46 | 只看该作者
全局:
nemoczech 发表于 2014-12-22 01:10
LZ能不能share下这题的题目名称是什么?谢谢了~

不知道叫什么啊 online 做的 不是demo题 没有名字的
回复

使用道具 举报

🔗
readman 2014-12-22 15:35:24 | 只看该作者
全局:
- = 我第一个想到的是斯特灵公式, 然后感觉那么复杂不会出..然后就不知道了
回复

使用道具 举报

🔗
readman 2014-12-22 15:39:08 | 只看该作者
全局:
- = 确实想不到不算出来n!就解决此题的方法...要是让算出来能有好多办法..
回复

使用道具 举报

🔗
 楼主| zenw 2014-12-23 08:23:54 | 只看该作者
全局:
readman 发表于 2014-12-22 15:39
- = 确实想不到不算出来n!就解决此题的方法...要是让算出来能有好多办法..

算出来肯定overflow了
回复

使用道具 举报

🔗
nemoczech 2014-12-24 01:42:27 | 只看该作者
全局:
有没有想出什么好的解法?我在网上搜了一圈似乎也没有什么满意的答案...顺便问问LZ做的是哪家公司的OA啊?
回复

使用道具 举报

🔗
 楼主| zenw 2014-12-24 05:07:30 | 只看该作者
全局:
xutopia 发表于 2014-12-24 02:49
Assuming the space complexity asked is O(N) word complexity (i.e. O(N) 32-bit integers or something) ...

大神 你认识徐硕 长起么? 我好像知道你
回复

使用道具 举报

🔗
lijie 2015-3-10 08:53:18 | 只看该作者
全局:
题目的名字是:FactorialDigitsSum
回复

使用道具 举报

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

本版积分规则

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