算法大挑战

fengjian_428 2009-04-06 09:14:25
加精
请编写以下函数 int MajorityElement(int array[],int n);
该函数返回数组array中的多数元素。多数元素是指在占绝对多数(至少51%)的一个值。如果多数元素不存在,那么返回常量NoMajorityElement,该函数必须满足下面的条件:
1. 必须以O(N)时间运行。
2. 必须使用O(1)的附加空间。换句话说,可用个别的临时变量,而不可以使用任何的临时数组。并且不能使用递归解决,这是因为随着递归层数加深,会需要空间来存储栈帧。
3. 不能改变数组中的任何元素的值。

大家一起讨论下啦
...全文
1605 117 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
117 条回复
切换为时间正序
请发表友善的回复…
发表回复
xnhsdp 2010-05-19
  • 打赏
  • 举报
回复
学习。。。。。。。。。。。。。
alleneros1984 2009-04-16
  • 打赏
  • 举报
回复
3:Code for Maximum Subset Sum

#include "limits.h"
#include "stdio.h"

int maxsub(int* arr, int len){

int i, maxsum = INT_MIN, sum=0;
int beg = 0, end = 0, pos = 0;

for (i=0; i<len; i++){

sum += arr[i];
if(sum > maxsum){

maxsum = sum;
beg = pos;
end = i;
}
if(sum < 0){

sum = 0;
pos = i+1;
}
}

for(i=beg; i<=end; i++)
printf("%d \t",arr[i]);
printf("\n");
return maxsum;
}
--小F-- 2009-04-14
  • 打赏
  • 举报
回复
学习一下
fengquansheng 2009-04-10
  • 打赏
  • 举报
回复
楼主:给你写个好算法你也领悟不了。
Mydal 2009-04-09
  • 打赏
  • 举报
回复
12楼的算法很不错啊 挺符合题目的
suiyili 2009-04-09
  • 打赏
  • 举报
回复
to remagon:

there is no recursive in my method, the two values N[p1] and N[p3] should be able to updated in a loop.
zexin1000 2009-04-09
  • 打赏
  • 举报
回复
路过,觉得很有挑战性。
qq441733920 2009-04-09
  • 打赏
  • 举报
回复
学习.
remagon 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 106 楼 suiyili 的回复:]
there is some mistake above, here is the correct one.

If an array N has S elements, then 51% means that there must be S*0.51 number of same elements.

Here, to be simple, suppose we have 100 elements, then ramdomly find an element, ok, we use the first element N[0], then, compared with N[0], go through every item in the array, after that we can get how many elements which are more than N[…
[/Quote]
递归了,而且要使用临时数组
suiyili 2009-04-09
  • 打赏
  • 举报
回复
yes, steal, you are absolutely right, thx.
steal8275756 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 106 楼 suiyili 的回复:]
Now we have new three parts (par1, between N[0] and N[p3], part2 equal to N[p3], part3, more than N[p3]).
at the same time, don't forget to pick up a value in part1 and part3 as new N[p1] and new N[p3] for next time compare.
continue above process, until find or fail.[/Quote]
红字部分应该是less than吧
ximengchang 2009-04-08
  • 打赏
  • 举报
回复
学习
小童012 2009-04-08
  • 打赏
  • 举报
回复
学习
suiyili 2009-04-08
  • 打赏
  • 举报
回复
there is some mistake above, here is the correct one.

If an array N has S elements, then 51% means that there must be S*0.51 number of same elements.

Here, to be simple, suppose we have 100 elements, then ramdomly find an element, ok, we use the first element N[0], then, compared with N[0], go through every item in the array, after that we can get how many elements which are more than N[0] (part 1), and how many are equal to it (part 2), how many are less than it(part3). When iterating the array, we randomly pick up N[p1] which is in part1 (N[p1] < N[0]) and N[p3] in part3 (N[0] < N[p3]).

if no number in the 3 part is more then 51, return false.

otherwise, if part2 >= 51, ok N[0] is the one.
if either part1 or part3 is more than 51, here suppose part3 is more than 51. since N[p3] in part3( N[0] < N[p3]), we can use both of values N[0] and N[p3] to go through the array again. this time, just focus on how many are between N[0] and N[p3], how many are equal to N[p3] and how many are more then N[p3].

Now we have new three parts (par1, between N[0] and N[p3], part2 equal to N[p3], part3, more than N[p3]).
at the same time, don't forget to pick up a value in part1 and part3 as new N[p1] and new N[p3] for next time compare.
continue above process, until find or fail.
suiyili 2009-04-08
  • 打赏
  • 举报
回复
Sorry my PC doesn't have chinese spell.

If an array N has S elements, then 51% means that there must be S*0.51 number of same elements.

Here, to be simple, suppose we have 100 elements, then ramdomly find an element, ok, we use the first element N[0], then, compared with N[0], go through every item in the array, after that we can get how many elements which are more than N[0] (part 1), and how many are equal to it (part 2), how many are less than it(part3). When iterating the array, we randomly pick up N[p1] which is in part1 (N[p1] < N[0]) and N[p2] in part2 (N[0] < N[p2]).

if no number in the 3 part is more then 51, return false.

otherwise, if part2 >= 51, ok N[0] is the one.
if either part1 or part3 is more then 51, here suppose part3 is more than 51. since N[p2] in part2( N[0] < N[p2]), we can use both of values N[0] and N[p2] to go through the array again. this time, just focus on how many are between N[0] and N[p2], how many are equal to N[p2] and how many are more then N[p2].

Now we have new three parts (par1, between N[0] and N[p2], part2 equal to N[p2], part3, more than N[p2]).
at the same time, don't forget to pick up a value in part1 and part3 as new N[p1] and new N[p2] for next time compare.
continue above process, until find or fail.
btsy2000 2009-04-08
  • 打赏
  • 举报
回复
注册一下~
Steve 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 86 楼 fengjian_428 的回复:]
12L的已经是正确的了, 对于题目要求:抓出大于50%
[/Quote]
嗯,>50%的话,52的反例也不成立.
12楼正确.
lfywy 2009-04-08
  • 打赏
  • 举报
回复
学习
qlzf11140820 2009-04-08
  • 打赏
  • 举报
回复
关注
鸿悯 2009-04-08
  • 打赏
  • 举报
回复
晕,这不是经典的主元素问题么,?
加载更多回复(95)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧