[LeetCode53]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

思路:题意为给定 n 个整数(可能为负数)组成的序列 a[1],a[2],a[3],...,a[n],求该序列如
a[i]+a[i+1]+...+a[j]的子段和的最大值。当所给的整数均为负数时定义子段和为
0,如果序列中全部是负数则最大子断和为0,依此定义,所求的最优值为 Max{0,a[i]+a[i+1]+...+a[j]},1≤i≤j≤n。
例如 输入:-2,11,-4,13,-5,-2
输出:20

则此题可通过动态规划求解

首先计算辅助数组。
接着计算辅助数组的最大值。

辅助数组b[j]用来记录一j为尾的子段和集合中的最大子断和。

例如,假如有一序列:-2,11,-4,13,-5,-2

          b(1) = -2 ,b(2) = 11, b(3) = 7, b(4) = 20, b(5) = 15, b(6) = 13
          a(1) = -2, a(2) = 11, a(3) = 7, a(4) = 13, a(5) =  -5, a(6) = -2
          b(1) < 0    b(2) > 0    b(3) > 0  b(4) > 0   b(5) > 0     b(6) > 0
---->
                        { b(j - 1) + a(j)                     当b(j-1) >= 0
              b(j) = {
                        {a(j)                                      当b(j-1) < 0

代码:

public class Solution {
    public int MaxSubArray(int[] nums) {
        int sum = 0;
        int[] dp = new int[nums.Length];
        int temp = 0;
        for(int i = 0; i < nums.Length; i++)
        {
            if(temp > 0)
                temp += nums[i];
            else
                temp = nums[i];
            dp[i] = temp;
        }
        sum = dp[0];
        for(int i = 0; i < dp.Length; i++)
        {
            if(sum < dp[i])
                sum = dp[i];
        }
        return sum;
    }
}
时间: 2024-10-24 22:35:27

[LeetCode53]Maximum Subarray最大字段和的相关文章

Leetcode:Maximum Subarray 最大字段和

戳我去解题 当从头至尾遍历数组时,对于数组中的每一个元素,有两种选择: 1.加入之前的subArray 2.舍弃之前的subArray,从该元素开始另起一个subArray 那么该如何确定选择执行哪一种情况呢? 如果之前subArray值大于0,那么我们可以认为这个subArray对以后的后续结果是有贡献的,因为促进正向增长,那么选择执行第1种情况 如果之前subArray值小于等于0,那么认为其对后续结果没有贡献,因为致使负向减少,这时选择执行第2种情况 设状态 f[j] 表示以 S[j]结尾

LeetCode53 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. (Medium) 分析: 遍历一遍,维护一个当前最大值t

[LeetCode][JavaScript]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. https://leetcod

【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 figu

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. 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.

41. leetcode 53. Maximum Subarray

53. 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. 思路:这个题还挺经典

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

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,