发现的最大时间间隔总和在实数的列表 [英] Find the maximum interval sum in a list of real numbers

查看:134
本文介绍了发现的最大时间间隔总和在实数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个面试问题,一个同事问一个编程的位置。我认为这是伟大的看受访者认为它通过。我很想得到的反应如何多所社区想到了。

Here's an interview questions that a colleague asked for a programming position. I thought this was great for watching the interviewee think it through. I'd love to get responses for how the SO community thinks of it.

鉴于长度为N实数的清单,说 [A_1,A_2,...,A_N] ,什么是找到了最大值M的复杂性其中存在指数1< = I< = J< = N使得

Given a list of real numbers of length N, say [a_1, a_2, ..., a_N], what is the complexity of finding the maximum value M for which there exist indices 1 <= i <= j <= N such that

A_I + A_ {I + 1} + ... + a_j = M

我的道歉,如果这是一个经典的CS问题。

My apologies if this is a classic CS problem.

推荐答案

的复杂性是只是为O(n)的Kadane算法

该算法跟踪试探性最大子的(maxSum,maxStartIndex,maxEndIndex)。它积累了部分和在 currentMaxSum 和更新的最佳范围时,这部分的总和变得比 maxSum 大。

The algorithm keeps track of the tentative maximum subsequence in (maxSum, maxStartIndex, maxEndIndex). It accumulates a partial sum in currentMaxSum and updates the optimal range when this partial sum becomes larger than maxSum.

这篇关于发现的最大时间间隔总和在实数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆