剑指 Offer 42. 连续子数组的最大和
一、题目详情
输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
要求时间复杂度为O(n)。
示例1:
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
提示:
1 <= arr.length <= 10^5
-100 <= arr[i] <= 100
二、动态规划法
class Solution {
public int maxSubArray(int[] nums) {
//动态规划 dp[i] 代表以元素 nums[i] 为结尾的连续子数组最大和(可以将原数组 nums 用作 dp 列表)
int max = nums[0];
for(int i = 1;i < nums.length;i++){
nums[i] = Math.max(nums[i - 1] + nums[i],nums[i]);
max = Math.max(max,nums[i]);
}
return max;
}
}