Maximum Subarray [最大子数组]

来源:互联网 发布:数据大玩家直播室 编辑:程序博客网 时间:2024/04/29 09:12

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

public class Solution {    public int maxSubArray(int[] A) {        if(A.length==0) return 0;        if(A.length==1) return A[0];        int start=A[A.length-1];        int maxSum=A[A.length-1];        for(int i=A.length-2;i>=0;i--){            start=Math.max(A[i],start+A[i]);            maxSum=Math.max(maxSum,start);        }        return maxSum;    }}
思路:dp

0 0
原创粉丝点击