41. Maximum Subarray

Description

Given an array of integers, find a contiguous subarray which has the largest sum.

The subarray should contain at least one number.

Example

Given the array [?2,2,?3,4,?1,2,1,?5,3], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

Challenge

Can you do it in time complexity O(n)?

这是一道求最大子串和的问题,想解决这个问题,我们需要考虑如下几种情况:

情况一:加上一个正数——那自然最好了,求知不得嘛。且慢,假如前几个数的和为负数,突然来了个正数,是把正数加入旧的队列中,还是这个正数自立门户呢

啥意思?例如,有如此序列 -1,3,5。。。。等等,你是直接从3开始呢,还是把-1这个拖油瓶带着?很明显,为了保证整个子串的值最大,应该从3开始,该思路

对应于下面代码中的注释一。

情况二:加上的数是一个负数——虽然说加上一个负数可能会变小,但有时候我们还是得加上的。例如:3,  4,  -5,  6, 7.......虽然加上-5会变小,但是它后面有更大的数,

能让整个序列得和变大。但有时候,加上了一个负数真的会使整个值变小,例如:3,-2,  1 我就三个数,加上-2后,哪怕再加上个一,相对于原来的3来说,还是变小了。

这个时候,就要求我们定义两个变量了,一个来保存最终的结果(res),另一个来“大胆地尝试”(curSum),如果一不小心curSum>res了,则更新res的值。代码如下:

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: A integer indicate the sum of max subarray
     */
    public int maxSubArray(int[] nums) {
        // write your code here
        int curSum=0;
        int res=Integer.MIN_VALUE;
        for(int num:nums){
            curSum=Math.max(curSum+num,num);//注释一
            res=Math.max(curSum,res);
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/phdeblog/p/9074054.html

时间: 2024-08-30 15:25:41

41. Maximum Subarray的相关文章

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 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. click to show more practice. Mor

[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.

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,

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: 采用滑