子序列在整型数组最大总和

编程入门 行业动态 更新时间:2024-10-14 00:27:04
本文介绍了子序列在整型数组最大总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于整数数组,你怎么能找到两个指数,i和j,使得在子数组起始元素的总和,并在指数结束最大化,线性时间?

Given an array of integers, how can you find two indices, i and j, such that the sum of the elements in the subarray starting and ending at the indices is maximized, in linear time?

推荐答案

从我的编程珍珠的:

maxsofar = 0 maxendinghere = 0 for i = [0, n) /* invariant: maxendinghere and maxsofar are accurate are accurate for x[0..i-1] */ maxendinghere = max(maxendinghere + x[i], 0) maxsofar = max(maxsofar, maxendinghere)

更多推荐

子序列在整型数组最大总和

本文发布于:2023-11-30 00:12:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   总和   序列   整型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!