Leetcode:Maximum Subarray 最大字段和

戳我去解题

当从头至尾遍历数组时,对于数组中的每一个元素,有两种选择:

1.加入之前的subArray

2.舍弃之前的subArray,从该元素开始另起一个subArray

那么该如何确定选择执行哪一种情况呢?

如果之前subArray值大于0,那么我们可以认为这个subArray对以后的后续结果是有贡献的,因为促进正向增长,那么选择执行第1种情况

如果之前subArray值小于等于0,那么认为其对后续结果没有贡献,因为致使负向减少,这时选择执行第2种情况

设状态 f[j] 表示以 S[j]结尾的最大连续字段和,则根据上述分析,可以得到状态转移方程:

f[j] = max{ f[j-1] + s[j],  s[j] }

res = max{ f[j] }

根据 f[j] = max{ f[j-1] + s[j],  s[j] } 我们也可以反推:

如果 f[j-1] > 0 那么加入之前subArray,对应第一种情况

如果 f[j-1] <= 0, 那么 s[j] >= s[j] + f[j-1], 则舍弃之前的subArray,另起,对应第二种情况


class Solution {
public:
int maxSubArray(int A[], int n) {
assert(A != NULL && n >= 0);
int maxSum = A[0];
int currSum = A[0];
for (int i = 1; i < n; ++i) {
if (currSum >= 0) {
currSum += A[i];
} else {
currSum = A[i];
}
if (currSum > maxSum) {
maxSum = currSum;
}
}
return maxSum;
}
};

此题需要注意 数组元素全为负的情况,所以 maxSum和currSum都初始化为数组第一个元素值

时间: 2024-11-13 07:57:12

Leetcode:Maximum Subarray 最大字段和的相关文章

LeetCode——Maximum Subarray

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. 原题链接: https://oj.leetcode.com/p

LeetCode: Maximum Subarray [052]

[题目] 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. [题意] 给定一个数组,找出和最大的子数组,返回这个

[leetcode]Maximum Subarray @ Python

原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: 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,

leetcode | Maximum Subarray 最大连续子序列的和

Maximum Subarray: https://leetcode.com/problems/maximum-subarray/ 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 [

LeetCode: Maximum Subarray 解题报告

Maximum Subarray 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. SOLUTION 1: 采用滑

[LeetCode] Maximum Subarray Sum

Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary o

leetcode Maximum Subarray 最大子序列

Maximum Subarray Total Accepted: 28381 Total Submissions: 83696 My Submissions 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 contiguo

LEETCODE —— Maximum Subarray [一维DP]

Maximum Subarray 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. More practice:

Leetcode: Maximum Subarray

题目: 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. More practice: If you have