由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 再来一道题
相关主题
Quick Sort的partition问题大家看看这几道google面试题怎么做?
~~~~~~~~问个G家的题~~~~~~~~~~~Re: 贡献个facebook电话interview
一个小公司面经问一道概念题
“常数空间O(N),O(1)算法那个题目”的变形题目Akamai店面一题
google 面试题疑问问几个老算法题的最佳解法
请问G一题discuss an array rearrange question
问一道F家的考古题The time complexity on finding the kth largest element in a
google老题:Find kth largest of sum of elements in 2 sorted array为什么这里的面试题和carrercup上的不一样呢
相关话题的讨论汇总
话题: occur话题: pass话题: algorithm话题: problem话题: values
进入JobHunting版参与讨论
1 (共1页)
p******n
发帖数: 32
1
一些面经那个帖子里的一道题
一个数组有k个不同数字,假设k是常数,如何O(n) time, O(1)space排序
m**k
发帖数: 290
2
My guess
First pass: count frequency
create an array, occur[k][2], occur[i][0] is the i-th unique number, occur
[i][1] is the frequency of this number.
time n, space 2xk
Second pass: sort
sort the array occur[k][2] with occur[i][0] as the key
time O(klogk)
Third pass: restore the sorted array
for i in 0:k-1
for j in 0:occur[i][1]
array[n++] = occur[i][0]
time n
Overall time 2n + O(klogk) = O(n), overall space 2xk = O(1)
y*c
发帖数: 904
3

这道题就是couting sort的in place 变种。counting好知道该放的位置之后,就是一
个juggling的过程。从后往前scan,碰到位置不对的,就放到该放的位置,然后循环。

【在 p******n 的大作中提到】
: 一些面经那个帖子里的一道题
: 一个数组有k个不同数字,假设k是常数,如何O(n) time, O(1)space排序

P*******7
发帖数: 55
4
You can only do one pass instead of several passes...

【在 y*c 的大作中提到】
:
: 这道题就是couting sort的in place 变种。counting好知道该放的位置之后,就是一
: 个juggling的过程。从后往前scan,碰到位置不对的,就放到该放的位置,然后循环。

s*******s
发帖数: 46
5
觉得如果O(1)的话很难做到one pass
several很简单
y*c
发帖数: 904
6

Why is that? It reads O(n). Do you have one pass algorithm for this problem?

【在 P*******7 的大作中提到】
: You can only do one pass instead of several passes...
P*******7
发帖数: 55
7
Similar to the problem with only two different values, we can extend that
algorithm to the problem with a constant number of different values with
one pass.
A special case with three different values is called "Dutch National Flag
Problem". http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/
You may start from there.

problem?

【在 y*c 的大作中提到】
:
: Why is that? It reads O(n). Do you have one pass algorithm for this problem?

y*c
发帖数: 904
8

I am interested in the fancy name but found it nothing but the "partition"
algorithm. Given K possible values, you need O(Kn) worst case even though it
is "one pass".

【在 P*******7 的大作中提到】
: Similar to the problem with only two different values, we can extend that
: algorithm to the problem with a constant number of different values with
: one pass.
: A special case with three different values is called "Dutch National Flag
: Problem". http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/
: You may start from there.
:
: problem?

p******n
发帖数: 32
9
Aglee. It is not worth using the "partition" algorithm when k > 3.
Counting algorithm is better in the sense of time complexity. In
addition, the code is more clean.

"partition"
though it

【在 y*c 的大作中提到】
:
: I am interested in the fancy name but found it nothing but the "partition"
: algorithm. Given K possible values, you need O(Kn) worst case even though it
: is "one pass".

1 (共1页)
进入JobHunting版参与讨论
相关主题
为什么这里的面试题和carrercup上的不一样呢google 面试题疑问
这题怎么做?请问G一题
怎么找均值相等的Two Partition of an array问一道F家的考古题
unsorted int array怎么找到第一个大于0,并且不在此array的数google老题:Find kth largest of sum of elements in 2 sorted array
Quick Sort的partition问题大家看看这几道google面试题怎么做?
~~~~~~~~问个G家的题~~~~~~~~~~~Re: 贡献个facebook电话interview
一个小公司面经问一道概念题
“常数空间O(N),O(1)算法那个题目”的变形题目Akamai店面一题
相关话题的讨论汇总
话题: occur话题: pass话题: algorithm话题: problem话题: values