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.

【题意】

给定一个数组,找出和最大的子数组,返回这个最大和

【思路】

之前去搜狗面试的时候被问到过这道题,典型的DP问题

从后向前依次确定以每一位开头的最大子串和

用A[i]表示i位置上的值,maxSum[i]表示i位置开头的最大子串的和

如果maxSum[i+1]<0,则maxSum[i]=A[i];

如果maxSum[i+1]>=0, 则maxSum[i]=A[i]+maxSum[i+1];

【代码】

class Solution {
public:
    int maxSubArray(int A[], int n) {
        if(n==0)return 0;
        if(n==1)return A[0];
        int max=A[n-1];
        int*maxSum=new int[n];
        maxSum[n-1]=A[n-1];
        for(int i=n-2; i>=0; i--){
            if(maxSum[i+1]<0)maxSum[i]=A[i];
            else maxSum[i]=A[i]+maxSum[i+1];
            if(maxSum[i]>max)max=maxSum[i];
        }
        return max;
    }
};

LeetCode: Maximum Subarray [052],布布扣,bubuko.com

时间: 2024-10-12 19:33:28

LeetCode: Maximum Subarray [052]的相关文章

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 @ 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

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 [㈢,1,3,4,ㄢ,2,1,5,4],the contiguous subarray [4,ㄢ,2,1] has the largest sum = 6. 经过多次尝试发现该问题本身没有最优子结构,但是其问题的转化:f(j