由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - an interview question
相关主题
Efficient algorithms for finding number, help please求助一个数据结构的求时间复杂度问题
两道M软件大公司的最新面世算法题 (转载)哪里有Introduction to Algorithms的problem的答案?
Algorithms and Data Structures那本比较好呢?[合集] 研究算法主要看哪本书? (转载)
问个简单的问题请推荐讲算法和数据结构的好书!
An interview question. Thanks.请教bit 操作
An algorithm question.今天面了个老印
find the subarray弱弱的问问常出现的让俺糊涂的关于顺序的表述(有包子送)!!(转载)
Interview questionC++新手求推荐算法书籍
相关话题的讨论汇总
话题: smallest话题: numbers话题: algorithm话题: question话题: interview
进入Programming版参与讨论
1 (共1页)
i*****y
发帖数: 1554
1
a. Given an array of n integers, can you write an algorithm that finds k
smallest numbers?
b. What is the complexity of your algorithm?
t*****l
发帖数: 121
2
能想到最好的办法就是建立一个k-element的max-heap.复杂度是O(nlogk)

k

【在 i*****y 的大作中提到】
: a. Given an array of n integers, can you write an algorithm that finds k
: smallest numbers?
: b. What is the complexity of your algorithm?

s****u
发帖数: 118
3
k smallest numbers是nth_element的副产品

k

【在 i*****y 的大作中提到】
: a. Given an array of n integers, can you write an algorithm that finds k
: smallest numbers?
: b. What is the complexity of your algorithm?

j****g
发帖数: 597
4
same idea as quick sort.
Use a pivot p as comparison base. All numbers larger than p move to right,
smaller move to left. Recurse on either left side with (k) or right side
with (k - #

Worst case is n^2.
No time for average case, but I guess should be O(n).

N********n
发帖数: 8363
5
A max-heap solution is faster than that.

【在 s****u 的大作中提到】
: k smallest numbers是nth_element的副产品
:
: k

p****n
发帖数: 148
6
why?

【在 N********n 的大作中提到】
: A max-heap solution is faster than that.
e*****w
发帖数: 144
7
O(N) time to select the k-th smallest element X. (see CLRS)
O(N) time to scan the array and compare with X to get the k smallest numbers.

k

【在 i*****y 的大作中提到】
: a. Given an array of n integers, can you write an algorithm that finds k
: smallest numbers?
: b. What is the complexity of your algorithm?

e*******e
发帖数: 32
8
第一步的副产品不就是答案了么?

numbers.

【在 e*****w 的大作中提到】
: O(N) time to select the k-th smallest element X. (see CLRS)
: O(N) time to scan the array and compare with X to get the k smallest numbers.
:
: k

1 (共1页)
进入Programming版参与讨论
相关主题
C++新手求推荐算法书籍An interview question. Thanks.
买了Sedgewick, Robert的Algorithm in C++An algorithm question.
javascript的一个问题:不能用loop,不能用library,怎么来remov (转载)find the subarray
[转载] CS Algorithm Interview questionInterview question
Efficient algorithms for finding number, help please求助一个数据结构的求时间复杂度问题
两道M软件大公司的最新面世算法题 (转载)哪里有Introduction to Algorithms的problem的答案?
Algorithms and Data Structures那本比较好呢?[合集] 研究算法主要看哪本书? (转载)
问个简单的问题请推荐讲算法和数据结构的好书!
相关话题的讨论汇总
话题: smallest话题: numbers话题: algorithm话题: question话题: interview