求子數組的最大和要求O(n),數組

來源:互聯網
上載者:User

求子數組的最大和要求O(n),數組
//求子數組的最大和

//輸入一個整形數組,有整數也有負數,數組中連續一個或多個子數組,每個子數組都有一個和,求所有子數組的和的最大值,要求時間複雜度O(n)



#include<iostream>int GetMax( int * arr){int max = arr[0];for (int i = 1; i < 10; i++){if (max < arr[i]){max = arr[i];}}return max;}int getMaxSum(int * arr){int result = 0;int tmp = 0;for (int i = 0; i < 10; i++){if (tmp>0){tmp += arr[i];}else{tmp = arr[i];}if (tmp > result)result = tmp;}return result;}using namespace std;void main(){int arr[10] = { 1, -3, 8, -6, 2, -3,4, 8,  -11, 12 };int max = GetMax( arr);if (max <= 0){cout << "最大子數組和為" << max;cin.get();return;}int result = getMaxSum(arr);cout << "最大子數組和為" << result;cin.get();}



演算法:子數組的最大乘積

這題你看清題目哎 不是求的子數組的最大乘積 編程之美上明明說是 給定一個長度為N的數組 求N-1個數的最大乘積

要不順便把我以前給你回答的問題結貼了?
 
演算法設計 二維數組子數組最大與的座標

應該用動態規劃,我覺得這個是最大欄位和問題的一個變型,現在沒環境,不方便貼代碼了,你也可以仔細思考下。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.