kadane算法_使用KADANE的算法求最大子陣列和

kadane算法

What is a sub-array?

什么是子陣列?

A sub-array is basically an array's contiguous part. For example, if we have an array of integers [1,2,3] so the sub-arrays that we can form from the given array are [1], [2] , [3] , [1,2] , [2,3] , [1,2,3].

子數組基本上是數組的連續部分。 例如,如果我們有一個整數數組[1,2,3],那么我們可以從給定數組形成的子數組為[1],[2],[3],[1,2],[ 2,3],[1,2,3]

So in the above example the sum of all the respective sub-arrays are 1,2,3,3,5,6. So here in this problem, we are required to find the maximum sub-array sum that could be obtained from a sequence of integers, which is 6 in the above case.

因此,在上面的示例中,所有各個子數組的總和為1,2,3,3,5,6 。 因此,在此問題中,我們需要找到可以從整數序列獲得的最大子數組和,在上述情況下為6

So many algorithms could be opted to solve the above problem, for example, a simple brute-force algorithm can be → that we can simply compute the sum of all the sub-arrays then loop through all those sums to compute maximum of those, but this algorithm will take O(N*N*N) time or in the best case O(N*N) if we try to do some pre-computation by making a cumulative some array also called a prefix sum array.

可以選擇很多算法來解決上述問題,例如,可以使用簡單的蠻力算法→這樣我們就可以簡單地計算所有子數組的總和,然后循環遍歷所有這些總和以計算出這些總和的最大值。如果我們嘗試通過使累積的某個數組也稱為前綴和數組來進行一些預計算,則此算法將花費O(N * N * N)時間,或者在最佳情況下為O(N * N)

So now why should we prefer KADANES's ALGORITHM?

那么,現在為什么我們應該選擇KADANES的算法

It is because this algorithm can solve our problem in O(N) time that is we can obtain the maximum sub-array sum in linear time complexity which is the most optimal solution for our task.

因為該算法可以解決O(N)時間的問題,所以我們可以獲得線性時間復雜度的最大子數組和,這是我們任務的最佳解決方案。

So let us now quickly try to understand the Kadane's Algorithm in two simple steps.

因此,讓我們現在通過兩個簡單的步驟快速嘗試理解Kadane算法

KADANE算法 (KADANE's algorithm)

  1. Initialize two variables named CS (current sum) and MS (max sum) with 0

    用0初始化兩個名為CS(當前和)和MS(最大和)的變量。

  2. Loop for each element of the array and do these below tasks for each iteration,

    循環訪問數組的每個元素,并為每次迭代執行以下任務,

    1. CS = CS + ar[i]
    2. If(CS<0) then CS=0
    3. (C) If(MS<CS) then MS=CS

Description: So basically if we have to find the maximum sub-array sum of a given array then the most optimal solution is KADANE'S ALGORITHM, which can easily perform the desired task in linear time complexity that is it will take O(N) time.

描述:因此基本上,如果我們必須找到給定陣列的最大子陣列總和,則最佳解決方案是KADANE算法 ,該算法可以輕松地以線性時間復雜度執行所需任務,這將花費O(N)時間。

SO now let us quickly jump to the coding part!

現在讓我們快速跳轉到編碼部分!

Code:

碼:

#include <iostream>
using namespace std;
int main()
{
cout<<"Enter the size of an array: ";
int n;
cin>>n;
int ar[100],cs,ms;
ms=0;cs=0;
cout<<"Enter the array elements:"<<endl;
for(int i=0;i<n;i++)
cin>>ar[i];
for(int i=0;i<n;i++)
{
cs+=ar[i];
if(cs<0)
{
cs=0;
}
ms=max(cs,ms);
}
cout<<"The maximum subarray sum is: "<<endl;
cout<<ms;
return 0;
}

Output

輸出量

Enter the size of an array: 6
Enter the array elements:
1 2 3 -4 6 -1
The maximum subarray sum is:
8  

翻譯自: https://www.includehelp.com/algorithms/find-the-maximum-sub-array-sum-using-kadanes-algorithm.aspx

kadane算法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542622.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542622.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542622.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java汽車油耗計算_轉發一個手機油耗計算器 (java)

今天在一個汽車論壇上看見的&#xff0c;試了試&#xff0c;還真不錯。比以前那個Fuel Consumption 功能要強大,雖然都是JAVA軟件。小羅盤手機計算器是作者獨自產品策劃、美術設計、程序開發、測試發布的手機應用軟件&#xff0c;是為作者的一個朋友寫的&#xff0c;當然我們用…

6.dubbo常用的xml配置有哪些_【面試篇】必須掌握的Spring 常用注解

閱讀文本大概需要5分鐘。注解本身沒有功能的&#xff0c;就和 xml 一樣。注解和 xml 都是一種元數據&#xff0c;元數據即解釋數據的數據&#xff0c;這就是所謂配置。本文主要羅列 Spring|Spring MVC相關注解的簡介。Spring部分1、聲明bean的注解Component 組件&#xff0c;沒…

Linux的iptables常用配置范例(2)

iptables -F #清除所有規則 iptables -X #清除所有自定義規則 iptables -Z #各項計數歸零 iptables -P INPUT DROP #將input鏈默認規則設置為丟棄 iptables -P OUTPUT DROP #將output鏈默認規則設置為丟棄 iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo …

aptitude 命令_C-命令行參數Aptitude問題與解答

aptitude 命令C programming Command Line Arguments Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Command Line Arguments – Passing values with running programs, separate argument values, number of argument…

文件上傳java邏輯_Java?文件上傳?實例

import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class Upload {private String saveDir "."; // 要保存文件的路徑private String contentType ""; // 文檔類型private String charset "";…

matlab數值計算pdf_Gnuplot科學繪圖(九)——柵格以及方程數值解估算

Gnuplot科學繪圖系列內容Gnuplot科學繪圖(一)——從安裝到簡單函數繪圖(文末有彩蛋)Gnuplot科學繪圖(二)——坐標取值范圍及刻度(文末有彩蛋)Gnuplot科學繪圖(三)——點線風格Gnuplot科學繪圖(四)——多組數據繪圖Gnuplot科學繪圖(五)——輸出eps 圖片Gnuplot科學繪圖(六)——輸…

dda算法_計算機圖形學中的DDA(數字差分分析儀)算法

dda算法DDA(數字差分分析儀)算法 (DDA (Digital Differential Analyzer) Algorithm) In computer graphics, the DDA algorithm is the simplest algorithm among all other line generation algorithms. Here, the DDA is an abbreviation that stands for "Digital Diff…

購物商城框架java_基于jsp的購物商城-JavaEE實現購物商城 - java項目源碼

基于jspservletpojomysql實現一個javaee/javaweb的購物商城, 該項目可用各類java課程設計大作業中, 購物商城的系統架構分為前后臺兩部分, 最終實現在線上進行購物商城各項功能,實現了諸如用戶管理, 登錄注冊, 權限管理等功能, 并實現對各類購物商城相關的實體進行管理。該購物…

c語言++數組名【數字】_C ++程序在數組中打印所有非重復數字

c語言數組名【數字】Problem statement: Write a C program to print all the non-repeated numbers in an array in minimum time complexity. 問題陳述&#xff1a;編寫一個C 程序&#xff0c; 以最小的時間復雜度將所有未重復的數字打印在數組中 。 Input Example: 輸入示例…

java最接近對點及距離_最接近點對問題_分治法

一、問題描述給定平面上的n個點&#xff0c;找其中的一對點&#xff0c;使得在n個點組成的所有點對中該點對間的距離最小。二、解題思路及所選算法策略的可行性分析思路&#xff1a;利用分治法來解決問題。遞歸子結構求最接近點對總體可分為幾個步驟&#xff1a;1、當問題規模小…

python return用法_初學Python要了解什么 裝飾器知識匯總有哪些

初學Python要了解什么&#xff1f;裝飾器知識匯總有哪些&#xff1f;在Python學習過程中&#xff0c;有多種方法對函數和類進行加工&#xff0c;相對于其它方式&#xff0c;裝飾器語法簡單&#xff0c;代碼可讀性高。因此&#xff0c;裝飾器在Python項目中有廣泛的應用&#xf…

android emulator虛擬設備分析第三篇之pipe上的qemud service

一、概述 本篇和第二篇是強相關的&#xff0c;需要結合第二篇一起看。 以boot-properties為例&#xff0c;注意不需要看ANDROID-QEMUD.TXT&#xff0c;這個是和guest os中的qemud進行相關的&#xff0c;已廢棄。 啟動emulator時&#xff0c;有一個參數-prop <key><val…

c#異常處理_C#異常處理能力問題和解答 套裝4

c#異常處理1) Which is not a valid keyword used in the context of exception handling? trycatchfinalfinally Answer & Explanation Correct answer: 3final The final keyword is not used to handle exceptions in C#.NET. 1)在異常處理的上下文中使用哪個無效關鍵字…

Castor xsd生成java_java – Castor可以處理從基礎XSD導入的多個XSD生成類嗎?

注意&#xff1a;我是EclipseLink JAXB (MOXy)領導者,也是JAXB 2 (JSR-222)專家組的成員.Can Castor do this? If so, what would be the Ant task syntax for it.If not, would perhaps JAXB be a better alternative?下面是如何使用JAXB完成此操作的示例&#xff1a;產品xm…

串口通信 校驗碼_一文讀懂S7-200 SMART自由口通信!

學習S7-200 SMART時了解到&#xff0c;基于RS485接口可實現一下幾種通信&#xff1a;1&#xff09;modbus RTU通信2&#xff09;PPI協議通信3&#xff09;USS協議通信4&#xff09;自由口通信何為自由口通信呢&#xff1f;前三種通信必須要PLC和與其通信的設備支持相同的通信協…

hbase 學習(十三)集群間備份原理

集群建備份&#xff0c;它是master/slaves結構式的備份&#xff0c;由master推送&#xff0c;這樣更容易跟蹤現在備份到哪里了&#xff0c;況且region server是都有自己的WAL 和HLog日志&#xff0c;它就像mysql的主從備份結構一樣&#xff0c;只有一個日志來跟蹤。一個master集…

python expect模塊_Python基礎教程:用Python怎么telnet到網絡設備

Python基礎教程&#xff1a;用Python怎么telnet到網絡設備0.前言Telnet協議屬于TCP/IP協議族里的一種&#xff0c;對于我們這些網絡攻城獅來說&#xff0c;再熟悉不過了&#xff0c;常用于遠程登陸到網絡設備進行操作&#xff0c;但是&#xff0c;它的缺陷太明顯了&#xff0c;…

Java實現動態加載頁面_[Java教程]動態加載頁面數據的小工具 javascript + jQuery (持續更新)...

[Java教程]動態加載頁面數據的小工具 javascript jQuery (持續更新)0 2014-05-07 18:00:06使用該控件&#xff0c;可以根據url&#xff0c;參數&#xff0c;加載html記錄模板(包含json參數對應&#xff0c;以及具體記錄位置Index根據參數描述加載對應的屬性&#xff0c;并可以…

馬哥linux第六周作業

1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄&#xff0c;將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#&#xff1b;[rootmageedu tmp]# cp /etc/rc.d/rc.sysinit . [rootmageedu tmp]# vim rc.sysinit :% s/^[[:space:]]/#&/ #按Esc進入vi…

Java ObjectInputStream enableResolveObject()方法與示例

ObjectInputStream類enableResolveObject()方法 (ObjectInputStream Class enableResolveObject() method) enableResolveObject() method is available in java.io package. enableResolveObject()方法在java.io包中可用。 enableResolveObject() method is used to enable th…