1. 程式人生 > >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.

最大子序列:

假設某個子序列a[1],a[2],a[3],a[4],....,a[n]為最大子序列,則a1+a2+....+ai>0恆成立,1 <= i <= n

否則可得到a[i+1] + a[i+2] + ....+ a[n] > a[1]+a[2]+....+a[n],即左邊是更大的子序列,與假設矛盾。

因此基於上述結論,從序列第一個元素開始計算sum。

令max表示sum中出現的最大值,同sum相同初始化為序列第一個元素。

若當sum<=0時,則拋棄前面的所有元素,從新一個元素開始計算sum

即如果sum = a[i]+a[i]+1+....+a[j]>0,而a[i] + a[i+1] + a[i+2] +...+a[j] + a[j+1] <= 0時,則令sum 成為一下元素值,即sum = aj+2。

直至遍歷一遍完成,時間複雜度為O(n)

具體程式碼如下

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int sum = nums[0];
        int max = sum;
        for(int i = 1; i < nums.size(); ++i) {
            if(sum <= 0) {
                sum = nums[i];
            }
            else{
                sum += nums[i];
            }
            max = max > sum ? max : sum;
        }
        return max;
    }
};




相關推薦

Maximum Subarray大子序列

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [

53. Maximum Subarray無序的大子序列

Find the contiguous subarray within an array (containing at least one number) which has the lar

152 Maximum Product Subarray 乘積大子序列

-s true www. com for pre pro return ems 找出一個序列中乘積最大的連續子序列(該序列至少包含一個數)。例如, 給定序列 [2,3,-2,4],其中乘積最大的子序列為 [2,3] 其乘積為 6。詳見:https://leetcode.co

(Java) LeetCode 152. Maximum Product Subarray —— 乘積大子序列

ann solution least posit 當前 res 暴力 根據 with Given an integer array nums, find the contiguous subarray within an array (containing at least

HDU1003 結題報告大子序列

HDU1003 Max Sum 題解 #include<iostream> #include<algorithm> #include<string> #include<math.h> #include<set> #in

PAT Maximum Subsequence Sum[大子序列和,簡單dp]

ace 序號 fin nts 很好 lag malle test 二次 1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }.

洛谷p4147 大子矩陣

#include<iostream> using namespace std; int hh[1010][1010],s[1010],l[1010]; char map[1010][1010]; int n,m,ans=0; void solve(int h[])

Avito Cool Challenge 2018:D. Maximum Distance 小生成樹

題目連結 題意 : 給出一個聯通圖和一些特殊的點,現在定義cost(u,v)為一條從u到v的路徑上面邊權的最大值 ,  定義dis(u,v) 為從u到v 路徑上面cost 的最小值 然後求所有特殊點到其他特殊點的最大距離   題解: 做這題前,首先思考一件事情,對於一顆樹來說

LeetCode 53. 大子序和 Maximum SubarrayC語言

題目描述: 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回其最大和。 示例: 輸入: [-2,1,-3,4,-1,2,1,-5,4], 輸出: 6 解釋: 連續子陣列 [4,-1,2,1] 的和最大,為 6。 進

leetcode 53 Maximum Subarray 大子陣列的和

題目要求 (高頻題) 給定一個整數陣列nums,找到具有最大和的連續子陣列(包含至少一個數字)並返回其和。 示例 Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the large

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 

PAT甲級--1007 Maximum Subsequence Sum (25)25 分大子序列和及其起始和終點】

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to

乘積大子序列 Maximum Product Subarray

空間分析 連續 思路 特殊情況 time imu complex 英文 輸入 [抄題]: 找出一個序列中乘積最大的連續子序列(至少包含一個數)。 比如, 序列 [2,3,-2,4] 中乘積最大的子序列為 [2,3] ,其乘積為6。 [暴力解法]: 時間分析:每次j循環時都

算法入門:大子序列和的四種算法Java

else 初始化 需要 nbsp ava 時間 pos sub for循環 最近再學習算法和數據結構,推薦一本書:Data structures and Algorithm analysis in Java 3rd 以下的四種算法出自本書 四種最大子序列和的算法: 問題描

hdu-1231 連續大子序列動態規劃

得到 繼續 用例 using 規劃 mem 空格 編寫 序號 Time limit1000 ms Memory limit32768 kB 給定K個整數的序列{ N1, N2, ..., NK },其任意連續子序列可表示為{ Ni, Ni+1, ..., Nj },其中

基於python的動態規劃經典問題爬樓梯,取珠寶,大子序列和,找零錢

1,爬樓梯問題 一個人爬樓梯,每次只能爬1個或兩個臺階,假設有n個臺階,那麼這個人有多少種不同的爬樓梯方法 動態規劃的狀態轉移:第 i 個狀態的方案數和第 i-1, i-2時候的狀態有關,即:dp[i]=dp[i-1]+dp[i-2],dp表示狀態矩陣。 def climb_stai

資料結構演算法題/大子序列一維陣列中和大的連續子序列

1首先看一下 最大子序列。 最大子序列是要找出由陣列成的一維陣列中和最大的連續子序列。比如{5,-3,4,2}的最大子序列就是 {5,-3,4,2},它的和是8,達到最大;而 {5,-6,4,2}的最大子序列是{4,2},它的和是6。你已經看出來了,找最大子序列的方法很簡單,只要前i項的和還沒有

[Swift]LeetCode152. 乘積大子序列 | Maximum Product Subarray

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1:

31.動態規劃-乘積大子序列-Leetcode 152python

題目描述 給定一個整數陣列 nums ,找出一個序列中乘積最大的連續子序列(該序列至少包含一個數)。 示例 示例 1: 輸入: [2,3,-2,4] 輸出: 6 解釋: 子陣列 [2,3] 有最大乘積 6。 示例 2: 輸入: [

LeetCode:53. Maximum Subarray找出陣列中和大的陣列

         Given an integer array nums, find the contiguous subarray (containing at least one number) which has the l