你特叔

GitHub: https://github.com/Hunter1023

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

 Given a sequence of K integers {N1,N2,...,NK}. A continuous subsequence is defined to be { Ni,Ni+1,...,Nj} 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;
import java.io.InputStreamReader;

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 {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // 获取整数的数量,以及具体的数字
        int cnt = Integer.parseInt(br.readLine());
        String[] nums = br.readLine().split(" ");
        br.close();
        int[] arr = new int[cnt];
        // 将具体数字转换成int型
        for (int i = 0; i < cnt; i++) {
            arr[i] = Integer.parseInt(nums[i]);
        }
        onlineProcess(arr);
    }
}
阅读更多
文章标签: Java 数据结构
上一篇01-复杂度1 最大子列和问题(20 分)
下一篇华为面试题-Session相关
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭