由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道算法题目
相关主题
问个最近面试里的题目问个算法题
关于质数(prime number)的算法题请教一道数据结构的设计题
L家的高频题merge k sorted arrays giving iterators求讨论!分享一道trading firm的code screen,只能用c++
贴个find kth prime number的CODE并请教。。。stable rearrange an integer array with + and -
问一道facebook的面试题问一个面试题
问道题: prime factorGoogle电话面试题目
array contains two integer that sum up to 7Amazon电面两题
问个题目,找不在区间内的所有数经典题:找前N个质数
相关话题的讨论汇总
话题: array话题: prime话题: sieve话题: numbers话题: 8k
进入JobHunting版参与讨论
1 (共1页)
w*****e
发帖数: 158
1
Q:Given an array of positive integers how to calculate how many prime
numbers are in the array? How do you improve it if you know the array size
is 100k and the numbers are between 2k to 8k.
哪位大侠给说说怎么improve 呀?多谢。
i**********e
发帖数: 1145
2
第一题可以用sieve来解决啊。http://en.wikipedia.org/wiki/Sieve_of_eratosthenes
我blog上也有贴过这问题,也贴了代码。
至于要怎么improve,你可以说清楚一些吗?是什么方面的improve呢?内存方面?还是
时间复杂度方面?

【在 w*****e 的大作中提到】
: Q:Given an array of positive integers how to calculate how many prime
: numbers are in the array? How do you improve it if you know the array size
: is 100k and the numbers are between 2k to 8k.
: 哪位大侠给说说怎么improve 呀?多谢。

w*****e
发帖数: 158
3
Thanks ihasleetcode,
Sieve method to find all prime numbers for n. By using it, my solution is
like following:
1. go through array A and find the maximum number MAX.
2. use sieve method to find all prime number smaller than MAX. and put
them
into a set (hashtable?)
3. go though the Array again and check whether each element is in the set.
~
O(nlogn) ?
If the array size is 100k and the numbers are between 2k to 8k. the
numbers in the set are the prime numbers in the range(2k,8k).
Correct me if I
m*****n
发帖数: 2152
4
第一问,估计是想知道几种方法可以做,随便提上几种就行;第二问,当然是sieve找
出prime hash table最快。
i**********e
发帖数: 1145
5
For the prime sieve method to work, you will need an array size of N (N is
the maximum number). Since you mentioned that the primes are between 2K and
8K, you only need array of size 8K for the prime sieve method to work.
Then you iterate through the 100K array one by one, and the prime sieve
array can tell you if a number is prime or not.
See below for code sample on how to implement. You might also want to read
Programming Pearls for a discussion of generating primes efficiently.
http://www.ih

【在 w*****e 的大作中提到】
: Thanks ihasleetcode,
: Sieve method to find all prime numbers for n. By using it, my solution is
: like following:
: 1. go through array A and find the maximum number MAX.
: 2. use sieve method to find all prime number smaller than MAX. and put
: them
: into a set (hashtable?)
: 3. go though the Array again and check whether each element is in the set.
: ~
: O(nlogn) ?

1 (共1页)
进入JobHunting版参与讨论
相关主题
经典题:找前N个质数问一道facebook的面试题
麻烦大家帮忙看一下求质数这段代码,求拍砖~问道题: prime factor
reverse an arrayarray contains two integer that sum up to 7
[合集] Google电话面试题目问个题目,找不在区间内的所有数
问个最近面试里的题目问个算法题
关于质数(prime number)的算法题请教一道数据结构的设计题
L家的高频题merge k sorted arrays giving iterators求讨论!分享一道trading firm的code screen,只能用c++
贴个find kth prime number的CODE并请教。。。stable rearrange an integer array with + and -
相关话题的讨论汇总
话题: array话题: prime话题: sieve话题: numbers话题: 8k