The description of the question:
Locate the consecutive sub-list in a list that has the highest sum of values.
For example:
Given a list: (-1, 4, -2, 3, 1, 2, -2), the sub-list to return should be (4, -2, 3, 1, 2) with sum of values being 8.
First thought of the question:
The solution is expected to be with time complexity O(n), in which 'n' is the length of the original list.
As it seems to me, an intuitive way of achieving that is looking through the incremental sum of the list.
Since the sum of values in sub-list is represented in the incremental sum by the difference between the value of start point (one before the beginning of the sublist) and end point, locating the sublist with highest sum is a matter of finding two points with highest forward difference.
So an algorithm to deal with the problem should on one hand store the lowest point so far (the trough) and the highest increase in value from the lowest so far by inspecting and keeping track of the difference between the current value and that lowest point. Hence the implementation may look like the following:
In the source code, currIncSum keeps track of the current incremental sum value with begin and end marking the beginning and end of the sub-sequence, lowestTrough is the lowest value so far with iTrough being the position of the point.
The logic in the first conditional branch updates the highest increase, corresponding to the sublist with highest sum so far. The second conditional branch updates the lowest point and its value.
A subtle aspect of the task the algorithm is to achieve is how to deal with the case in which all values of the list are negative. Depending on the demand, the algorithm can return zero-lengthed list or the entry with highest value in the list as the sub-list. The above code implements the latter, with its recSum, the highest jump so far initialized to the minimal integer value.
Another way of handling the problem is simpler, and most likely more efficient but not as straightforward at a first glance. The code is as follows,
The idea is borrowed from (and a similar piece of code in C is provided by) July's blog post at:
http://blog.csdn.net/v_JULY_v/archive/2011/05/25/6444021.aspx
The key of this implementation is it exploited the fact that the candidate beginning point of the sublist only needs to be changed after the sum of current sublist is lower than zero (corresponding to in the perspective of the former algorithm, a new minimum value is found). The addition operation for the sum in this algorithm is reset, suspended and turned to an inspection of incoming value after such point is reached until the value turns to positive again. The process of recording the highest jump is performed in a separate logic that follows which keeps track of the sum value provided by the preceding logic. So the logic that generates the jump theoretically needs only to ensure it does not miss such a maximum value.
From this simple but typical example, we can see algorithmic problems need special attention from developers for them to make constant improvement in simplicity, efficiency, etc. and some of the changes can't be easily seen and require certain in-depth understanding, experience, changes in ways of thinking and even talents.
分享到:
相关推荐
《A Classical Introduction to Modern Number Theory》(《现代数论的经典入门》)是由已故的肯尼斯·爱尔兰(Kenneth Ireland)和迈克尔·罗森(Michael Rosen)共同著作的数论入门书籍。数论,作为数学的一个分支...
《A Classical Introduction to Cryptography Exercise Book》是一本由Thomas Baignères、Pascal Junod、Yi Lu、Jean Monnerat以及Serge Vaudenay等五位来自瑞士洛桑联邦理工学院(EPFL)的学者共同编写的关于密码...
标题中的"classical_music_1.zip_8元 阵_Classical Music_DOA_DOA 分辨率_DOA估计"提到了几个关键概念,分别是8元阵列、古典音乐、DOA(Direction of Arrival)以及DOA估计和分辨率。这些概念与信号处理、声学和音频...
**系列**:The Loeb Classical Library系列(希腊作者) **索书号**:QA37.A3T5 V.1-2 《希腊数学史选读》选取了大量古代希腊数学家的作品,并提供了英文翻译版本。这些作品不仅反映了古希腊数学的高度发展水平,也...
- 构造导向矢量 \(\mathbf{a}(\theta)\),并通过计算谱值 \(P(\theta) = \frac{1}{|\mathbf{a}^H(\theta) \mathbf{E}_n|^2}\) 来确定DOA估计。 #### 4. 利用ICA进行源信号分离 - 在每个频率分量上单独应用ICA算法,...
- **标题**: "A classical thesis on Optical Flow Estimation" 本篇博士论文以经典著称,聚焦于光流估计(Optical Flow Estimation)领域。光流估计是一种计算机视觉中的关键技术,用于分析图像序列中像素的运动...
### 《古典電動力學習題解答指南》——J.D. Jackson第三版伴讀 #### 概述 本書作為J.D. Jackson所著《古典電動力學》第三版的伴讀指南,旨在幫助學生深入理解並掌握書中的理論與實踐問題。原書因其詳盡、深入的...
Classical Electricity and Magnetism
第二定律(动力定律)定义了力和加速度之间的关系,即F=ma,其中F是作用在物体上的合外力,m是物体的质量,a是加速度;第三定律(作用与反作用定律)说明了对于每一个作用力都存在一个大小相等、方向相反的反作用力...
Schwinger J-Classical Electrodynamics (Perseus 1998)
如果质量\( m \)保持不变,则简化为\( F = ma \),其中\( a = \frac{dv}{dt} = \frac{d^2r}{dt^2} \)表示加速度。 - **牛顿第二定律**:在一个惯性参考系中,物体所受合力等于其质量与加速度的乘积,即\( F = ma \)...
A simple java version of a classical mobile game DefendTheCarrot(经典塔防游戏《包围萝卜》的java仿制版)A simple java version of a classical mobile game DefendTheCarrot(经典塔防游戏《包围萝卜》的java...
A prior course on state-spacetheory would be advantageous for some optional sections, but is not necessary.To keep the development elementary,the systems are single-input/single-output and linear, ...
Classical Mechanics Vol.2
#### 标题:Goldstein Classical Mechanics 习题解答 **Goldstein** 的《Classical Mechanics》是一本在物理学领域内极具权威的经典教材,广泛应用于大学物理课程的教学与研究中。该书涵盖了经典力学的各个方面,...
### SVA Classical Q&A #### 1. 什么是回调(Callback)? 在系统级验证语言(SystemVerilog, SV)中,回调是一种特殊的类方法,它允许用户定义的行为在某些特定事件发生时被自动调用。这通常用于实现验证组件之间的...
Classical Mechanics (3rd Edition) ·作者:Goldsten.H.、Poole.C.、Safko.J. ·页码:638 页 ·出版日期:2005年 美国哥伦比亚大学HerbertGoldstein编著的《经典力学》(ClassicalMechanics)是一本有着很高知名...