Redian新闻
>
mmd,新版current 真难看,已经删除了,
avatar
mmd,新版current 真难看,已经删除了,# PDA - 掌中宝
k*t
1
Divide a list of numbers into group of consecutive numbers but their
original order should be preserved? e.g.
8,2,4,7,1,0,3,6
2,4,1,0,3 and 8,7,6
obviously in shortest time and space.
=========================================
看到一个comment,但不确定其中的find/union是否可以O(n)实现。
1. Identify range - One pass
2. Identify number of elements within each range - One pass
3. Fill destination array by maintaining starting positions of each range -
One pass of source array.
O(n) time and O(n) space
avatar
d*e
2
真是快剥皮了,denial 也好 approval 也好 给个结果 就这么苦等 太折磨人了
avatar
o*1
3
avatar
k*f
4
有谁推荐个好用的,主要看cnbeta,slickdeals。
avatar
p*2
5
8,2,4,7,1,0,3,6
第一遍得到最小值0, 最大值8
bool[] flags=new bool[9];
第二遍填flags, {1,1,1,1,1,0,1,1,1}
第三遍得到两个group 0-4 6-8
开两个list
第四遍,把每个数填到相应的list里
avatar
c*o
6
pat pat, 根据dolstats,现在处理到8月9号了,马上就有信儿啦,坚持住!
avatar
B*r
7
n
avatar
k*t
8
1. What if a[4]=100000? Not O(n) space any more, but O(Max) space.
2. In step 4, for given i=0..n-1, how do we know which list/group a[i]
belongs to?

【在 p*****2 的大作中提到】
: 8,2,4,7,1,0,3,6
: 第一遍得到最小值0, 最大值8
: bool[] flags=new bool[9];
: 第二遍填flags, {1,1,1,1,1,0,1,1,1}
: 第三遍得到两个group 0-4 6-8
: 开两个list
: 第四遍,把每个数填到相应的list里

avatar
R*d
9
bless

【在 d*******e 的大作中提到】
: 真是快剥皮了,denial 也好 approval 也好 给个结果 就这么苦等 太折磨人了
avatar
b*n
10
my 2 cents:
It seems O(n) is not possible, should be at least O(nlgn). Worst case
scenario, all the numbers belong to their own group, meaning no two numbers
are consecutive. Then each number has to be compared with existing group to
find out whether it belongs to them or has to be in a new group of his own.
Anything based on comparison like this has a low bound of O(nlgn). Of course
, if the storage can be O(Max), then O(n) time can be realized, similar to
the counting algorithm.
avatar
p*2
11
当然是要有条件的。就是number的范围有限。
in step 4, 可以用binary search, 如果group数目小的话,可以忽略。如果想得纯O(n
),可以用hashtable.

【在 k***t 的大作中提到】
: 1. What if a[4]=100000? Not O(n) space any more, but O(Max) space.
: 2. In step 4, for given i=0..n-1, how do we know which list/group a[i]
: belongs to?

avatar
k*t
12
以前看见一个版上大拿做的一个O(N)的find/union。不过那个是找
最长连续子序列,所以并集时只需更新边界元素,且并集操作只会
在边界发生。动态更新最长序列,只需扫描一遍即可。
下面是原贴。不知可否借鉴得上?
发信人: battlesc (zerg), 信区: JobHunting
标 题: Re: G题讨论
发信站: BBS 未名空间站 (Fri Jul 15 11:37:33 2011, 美东)
#include
#include
using namespace std;
int main() {
//int a[8] = {101,2,3,104,5,103,9,102};
int a[9] = {100,3,200,1,2,4,3,6,7};
// Assume at least one number
int L = sizeof(a)/sizeof(a[0]);
cout << "total: " << L << endl;
map ht;
int max = 1;
int start = a[0], end = a[0];
// Scan and store number of consecutive numbers
for (int i = 0; i < L; ++i)
{
if (ht.find(a[i]) != ht.end())
continue;

int c1 = 0;
if (ht.find(a[i]+1) != ht.end())
c1 = ht[a[i]+1];
int c2 = 0;
if (ht.find(a[i]-1) != ht.end())
c2 = ht[a[i]-1];
// Update start and end in the map for the group a[i] belongs to
int newCount = c1 + c2 + 1;
ht[a[i]] = newCount;
ht[a[i]+c1] = newCount;
ht[a[i]-c2] = newCount;

// Record max
if (newCount > max)
{
max = newCount;
start = a[i] - c2;
end = a[i] + c1;
}
}
cout << start << "..." << end <return 0;
}

-
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。