## 你特叔

GitHub: https://github.com/Hunter1023

# 01-复杂度2 Maximum Subsequence Sum（25 分）

Given a sequence of K integers {${N}_{}1,{N}_{}2,...,{N}_{}K$$N_​1, N_​2, ..., N_​K$}. A continuous subsequence is defined to be { ${N}_{}i,{N}_{i+1},...,{N}_{}j$$N_​i, N_{​i+1}, ..., N_​j$} where 1 ≤ i ≤ j ≤ K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

## 思路

• 序列中有正数 → 返回：最大子列和 以及 首尾元素
• 序列都是负数 → 返回：0 和 首尾元素
• 序列中只有负数和0 → 返回：0 0 0

## 代码

import java.io.BufferedReader;
import java.io.IOException;

public class Main {

public static void onlineProcess(int[] arr) {
int thisSum = 0, maxSum = 0;// 初始化当前和、最大和为0
int firstNum = arr[0], lastNum = arr[arr.length-1];//初始化子序列的首尾两个数。
boolean isFirst = true;//判断是否为最大子列的第一个数

boolean isZeroMax = true;//判断0是不是序列里最大的数
int cntOfNegative = 0;//统计负数数量

for(int i = 0; i < arr.length; i++) {
if(arr[i] > 0) {//如果序列里有正数，0就不是最大的
isZeroMax = false;
break;
}
if(arr[i] < 0) {
cntOfNegative++;
}
}
//如果只有负数和0
if(cntOfNegative < arr.length && isZeroMax) {
System.out.println(maxSum +" 0 0");
}else {
int cntOfSequence = 0;//统计子列的元素数量
for (int i = 0; i < arr.length; i++) {

thisSum += arr[i];// 向右累加
cntOfSequence++;//统计累加的数量
if (thisSum > maxSum) {
maxSum = thisSum;
if(isFirst) {
firstNum = arr[i - cntOfSequence + 1];//更新最大子列的第一位数
isFirst = false;
}
lastNum = arr[i];//更新最大子列的最后一位数
} else if (thisSum < 0) {//如果当前子列为负，因为不可能使后面的部分和增大，所以重新计算部分和。
thisSum = 0;
isFirst = true;
cntOfSequence = 0;
}
}
System.out.println(maxSum +" "+ firstNum + " " + lastNum);
}
}

public static void main(String[] args) throws IOException {
// 获取整数的数量,以及具体的数字
br.close();
int[] arr = new int[cnt];
// 将具体数字转换成int型
for (int i = 0; i < cnt; i++) {
arr[i] = Integer.parseInt(nums[i]);
}
onlineProcess(arr);
}
}

#### MOOC ：01-复杂度2 Maximum Subsequence Sum

2015-10-07 21:55:40

#### PAT 数据结构 01-复杂度2. Maximum Subsequence Sum (25)

2015-07-13 10:29:53

#### 01-复杂度2.Maximum Subsequence Sum

2015-03-05 19:17:25

#### 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

2015-09-02 21:30:25

#### 01-复杂度2. Maximum Subsequence Sum (25)

2015-03-15 00:25:33

#### 算法笔记-1-最大子列和-Maximum Subsequence Sum

2016-09-18 22:58:39

#### 1007. Maximum Subsequence Sum (25) -- 动态规划

2015-08-13 21:11:58

#### 01-复杂度2 Maximum Subsequence Sum (25分)

2015-10-23 22:28:56

#### pta 01-复杂度2 Maximum Subsequence Sum (25分)

2015-09-01 22:44:04

#### PAT-Java-1007. Maximum Subsequence Sum (25)

2018-01-13 17:24:24