把一串数(32位int型)放到Num中,求begin和end位置使得begin与end之间的是数字和最大,要求时间复杂度是O(n)。
注:不可以先排序,这串数字的位置不能改变。
最好有源码,思路也可以。
要求时间复杂度为O(n)的求两个位置之间最大值的算法
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- IT_Linux 2015-01-22 01:51关注
#include
#includeint getmax(int first, int second)
{
return first > second ? first : second;
}int main()
{
int start_index = 0, end_index = 0;
int sum_max = 0, result = 0;int index = 0; int data[] = {1, 3, -5, 0, 6, -4, 9, 12, -1}; int count = sizeof(data)/sizeof(int); for(index = 0; index < count; index++) { sum_max = getmax(0, sum_max) + data[index]; if(data[index] >= sum_max) { start_index = index; } result = getmax(result, sum_max); if(result == sum_max) { end_index = index; } } printf("min section [%d, %d],sum : %d\n", start_index, end_index, result); return 0;
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥15 python变量和列表之间的相互影响
- ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
- ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
- ¥15 CSAPPattacklab
- ¥15 一直显示正在等待HID—ISP
- ¥15 Python turtle 画图
- ¥15 关于大棚监测的pcb板设计
- ¥15 stm32开发clion时遇到的编译问题
- ¥15 lna设计 源简并电感型共源放大器
- ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)