1. 程式人生 > >LeetCode | Maximum Subarray(連續最大子陣列)

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 figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.


題目解析:

這道題之前已經總結過了:最大連續子陣列和。但可能當時沒理解到位,導致動態規劃時又出錯誤,分不清什麼時候判斷sum。

利用動態規劃思想,sum[i]表示以A[i]結尾的最大連續子陣列和。好,那麼sum[i]什麼時候最大?sum[i] = max(sum[i-1]+A[i],A[i])。再進一步化簡的話,就是sum[i-1]是否小於0,當小於0時,sum[i] = A[i]。也就是求出的sum[i]一定包含A[i]的值。

所以在for迴圈中一開始就判斷sum[i-1]的值。又因為求sum[i]只和前一個值有關係,又可以進一步化簡,只用一個變數,而不用申請一段空間。

class Solution {
public:
    int maxSubArray(int A[], int n) {
        if(n<=0)
            return 0;
        int sum = A[0];
        int max = A[0];
        for(int i = 1;i < n;i++){
            sum += A[i];
            if(sum < 0){
                sum = 0;
            }
            if(sum > max)
                max = sum;
        }
        return max;

    }
};


方案二:分治法

題目中要求用分治策略,在上面連結中已經有了,要注意跨越中間點時,左右兩邊一定要加上資料。

int max_sum_array2(int arr[],int low,int high,int *left,int *right)
{
    if(low == high){    //遞迴結束點
        *left = low;
        *right = high;
        return arr[low];
    }
    int mid = (low+high)/2;
    int lleft,lright,rleft,rright,mleft,mright;
    //三種情況下分別求解。
    int lmax = max_sum_array2(arr,low,mid,&lleft,&lright);
    int rmax = max_sum_array2(arr,mid+1,high,&rleft,&rright);
    int mmax = find_max_crossing_subarray(arr,low,mid,high,&mleft,&mright);
    //返回最大值
    if(lmax > rmax && lmax > mmax){
        *left = lleft;
        *right = lright;
        return lmax;
    }else if(rmax > lmax && rmax > mmax){
        *left = rleft;
        *right = rright;
        return rmax;
    }else{
        *left = mleft;
        *right = mright;
        return mmax;
    }
}

int find_max_crossing_subarray(int arr[],int low,int mid,int high,int *left,int *right)
{
    int lsum = -100;
    int sum = 0;

    for(int i=mid;i>=low;i--){  //這裡的演算法類似一趟暴力演算法,求出最大值
        sum += arr[i];
        if(sum > lsum){
            lsum = sum;
            *left = i;
        }
    }

    int rsum = -100;
    sum = 0;
    for(int j=mid+1;j<=high;j++){
        sum += arr[j];
        if(sum > rsum){
            rsum = sum;
            *right = j;
        }
    }

    return lsum+rsum;
}


相關推薦

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 

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 題解之 53. Maximum Subarray連續陣列大和問題

53. Maximum Subarray(連續子陣列的最大和問題) 題目描述和難度 題目描述: 給定一個整數陣列 nums ,找到一個具有最大和的連續子陣列(子陣列最少包含一個元素),返回

[LeetCode] Continuous Subarray Sum 連續的子陣列之和

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sum

[LeetCode] 628. Maximum Product of Three Numbers 三個數字的大乘積 [LeetCode] 152. Maximum Product Subarray大子陣列乘積 All LeetCode Questions List 題目彙總

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6  Example 2

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

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

leetcode 53. Maximum Subarray 大子陣列 分治演算法

題目 給定一個整數陣列,找出和最大的連續子陣列(至少包含一個元素)並返回最大和。 嘗試使用分治演算法實現 思路 參考了《演算法導論》裡關於分治的講解,正好就是這個題目,不過它的方法比較細緻,把最大子陣列的位置也求出來了。這道題只要求最大和,我簡化了一下。思路是

[LeetCode] Maximum Size Subarray Sum Equals k 大子陣列之和為k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: G

[LeetCode] Maximum Product Subarray大子陣列乘積

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the

LeetCode 53. 大子序和 Maximum SubarrayC語言

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

動態規劃法大子陣列問題maximum subarray problem

問題簡介   本文將介紹計算機演算法中的經典問題——最大子陣列問題(maximum subarray problem)。所謂的最大子陣列問題,指的是:給定一個數組A,尋找A的和最大的非空連續子陣列。比如,陣列 A = [-2, -3, 4, -1, -2, 1

643. Maximum Average Subarray I 大子陣列平均數

bject ble height r12 for over xpl example padding Given an array consisting of n integers, find the contiguous subarray of given length k

LeetCode:581. Shortest Unsorted Continuous Subarray找出陣列中不需要排序的陣列

      Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order

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

大子陣列連續數組的大和

HA ati pan AC () 個數 code 少包 CA 問題描述 在一個數組中找出和最大的連續幾個數(至少包含一個數)。 例如: 數組 A[] = [?2, 1, ?3, 4, ?1, 2, 1, ?5, 4],則連續的子序列[4,?1,2,1]有最大的和6。 輸入格

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

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

返回一個二維整形陣列中的大子陣列的和隨機二維整形陣列

一、題目:返回一個二維整數陣列中的最大子陣列的和(隨機二維整形陣列) 二、課題要求: 輸入一個二維整形陣列,數組裡有正數也有負數; 二維陣列中連續的一個子矩陣組成一個子陣列,沒個子陣列都有一個和; 求所有子陣列的和的最大值,要求時間複雜度為O(n)。 三、結對程式設計要求: 兩人結對完成程式設計任

用c++實現環形陣列大子陣列之和結對

結對作業 1.分解問題,將環形陣列,剪開變成一個一維陣列。 2.用一維陣列的最大子陣列和解決。 對於一個環形陣列,對每一個一維陣列的表示共有n-1種 原始碼如下: 1 #include<iostream> 2 using namespace std; 3 int max_