“常用公式”在线计算,“设计手册”在线查询
For example,if the input is[1,‐3,5,‐2,9,‐8,-6,4],the output would be[5,‐2,9].
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 支持支持 反对反对

共 1 个关于本帖的回复 最后回复于 2013-7-30 17:53

沙发
孙宏雷 新来的 发表于 2013-7-30 17:53:33 | 只看该作者
研发埠培训中心
Good Answer:Observe that the sum of a subarray from element i to element j isequal to the sum of the subarray from element 1 to element j minus the subarrayfrom element 1 to element i‐1.Our algorithm will iterate through the array.Thealgorithm keeps track of the sum x of the elements no later than the element.It willalso keep track of the minimum sum y of the subarray from the first element to anelement no later than the current element.Finally,It will also keep track of thesubarray z with the maximum sum so far.A teach step,we update x by adding thecurrent element to it.We updatey by checking whether x<y; if so, we set y to be x.We update z by checking whether y‐x is greater than z;if so,we set z to be y‐x.For example,with the sample input,our algorithm would do thefollowing:Current element | x | y | z---------------------------------- 1 |1 | 0 | 1-3 | -2 | -2 | 0 5 |3 | -2 | 5-2 | 1 | -2 | 5 9 |10 | -2 | 12-8 | 2 | -2 | 12-6 | -4 | -4 | 12 4 |0 | -4 | 1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们

360网站安全检测平台