Find Maximum Subarray O(n)

最大子数组分析O(n)

对于一个数组,数组中有正有负,求最大子数组

1,         该数组只可能从一个正数开始

2,         在从这个元素p1挨个求和,记录这个过程中的最大和

3,         如果这个和加到元素n1等于0了,那么整个数组的最大子数组和,要么就是上面中出现过的最大和,要么就在此n1之后的子数组中,不可能是从p1到n1之间的某个元素开始的子数组!

因为假设n存在于[p1,n1],从n到p才是真正的最大子数组,那么由于p1到n(不包括n)的和大于0,可以把数组扩展到p1使其更大,这就矛盾了,所以如果不是刚才已遍历的元素中最大的,必然是其后子数组中最大的。

4,         然后我们可以从n1之后一个整数重复2到3,找最大和

5,         最后,真正的最大子数组就是在这个过程中遇到的最大和对应的子数组

6,         因为我们只是一次遍历数组,每次遍历都是常数时间,所以整个过程的时间复杂度就是theta(n)

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> vi = { 13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7 };
    int max_sum = 0;
    int max_left = 0, max_right = 0;
    int sum = 0;//临时的sum,left,right;
    int left = -1,right=-1;
    while (vi[++left] <= 0);//找到第一个起点,循环left+1次
    max_left = left;
    for (right = left;right<vi.size();++right) {//循环vi.size()-left次
        sum += vi[right];
        if (sum <= 0) {
            sum = 0;//重新来过
            left = right+1;//因为right已经在循环中自加了
        }
        if (max_sum < sum) {
            max_sum = sum;
            max_right = right;
            max_left = left;
        }
    }
    //总过循环了vi.size()+1,每个循环体都是常数时间就可完成,所以运行时间为theta(n)
    cout << max_sum << " " << max_left << " " << max_right << endl;

}
时间: 2025-02-01 11:06:16

Find Maximum Subarray O(n)的相关文章

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

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

[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Example For [1, 2, -3, 1], return 6. Note The subarray should contain at leas

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a