当前位置:网站首页>Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
2022-06-12 09:00:00 【Love you, little pig】
List of articles
One 、 Title Description
Enter an integer array , One or more consecutive integers in an array form a subarray . Find the maximum sum of all subarrays .
The required time complexity is O(n).
Example :
Input : nums = [-2,1,-3,4,-1,2,1,-5,4]
Output : 6
explain : Continuous subarray [4,-1,2,1] And the biggest , by 6.
Tips :
1 < = a r r . l e n g t h < = 1 0 5 \rm{1 <= arr.length <= 10^5} 1<=arr.length<=105
− 100 < = a r r [ i ] < = 100 \rm{-100 <= arr[i] <= 100} −100<=arr[i]<=100
Two 、 Thought analysis
Ideas
In the previous data structure blog ( Link to : The maximum sum of successive subarrays : Three methods ) There are three ways to solve the maximum sum of continuous subsequences , Its time complexity and space complexity are as follows :
The time complexity required for the topic is O(N), So we use dynamic programming .
State definition : Set up a dynamic programming listdp,dp[i]Represented by elementsnums[i]Is the maximum sum of consecutive subarrays at the end .
Transfer equation : ifdp[i-1]≤0, explaindp[i-1]Yesdp[i]Produce negative contribution , namelydp[i-1]+nums[i]Not so goodnums[i]Itself big .
---- Whendp[i-1]>0when : performdp[i]=dp[i-1]+nums[i]
---- Whendp[i-1]≤0when : performdp[i]=nums[i]
The initial state :dp[0]=nums[0], That is tonums[0]The maximum sum of consecutive subarrays at the end isnums[0].
Return value : returndpThe maximum value in the list , Represents the global maximum .
Reduced space complexity :
becausedp[i]Only withdp[i-1]andnums[i]It matters , Therefore, the original array can benumsUsed as adplist , That is, directly innumsYou can modify it on the . As omitteddpExtra space used by the list , So the spatial complexity goes fromO(N)Down toO(1).
case analysis :
Complexity analysis :
Time complexity O ( N ) \rm{O(N)} O(N): Linear traversal arraynumsYou can get results , UseO(N)Time .
Spatial complexity O ( 1 ) \rm{O(1)} O(1): Use extra space of constant size .
3、 ... and 、 The overall code
The overall code is as follows
int maxSubArray(int* nums, int numsSize){
int max = nums[0]; // Used to record the largest child columns and
int tmp = nums[0]; // Used to record dp[i] Value
int former = 0; // Used to record dp[i-1] Value , about dp[0] for , In front of the dp[-1]=0
for(int i = 0; i < numsSize; i++){
tmp = nums[i]; // Record the value of the current item
if (former > 0) tmp+=former; // If dp[i-1] Greater than 0, Then add dp[i-1]
if (tmp > max) max=tmp; // If tmp Greater than max, Will max Assign a value to tmp
former = tmp; // Move to next , to update former value
}
return max;
}
function , The test passed 
边栏推荐
- 解压缩zip文件的工具类
- Dynamically create and submit forms
- MySQL learning record - II. MySQL create table command
- MFS explanation (IV) -- MFS management server installation and configuration
- ISCSI详解(五)——ISCSI客户端配置实战
- 2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
- Unittest测试框架
- Graphic analysis of viewbox in SVG
- (node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
- MySQL learning records -- III. MySQL query statements
猜你喜欢

Background color translucent

node示例后台搭建

Unittest test framework

Union selector

网页中加载二次元3D虚拟主播源码(1:项目介绍和源码)

The classic dog contract of smart contract (I)
![Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps](/img/0a/65bc44850e52204af278e50a8f86eb.jpg)
Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps

Domain name mapping to specified IP
![Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock](/img/42/000a3e601ba1771a1ee07fcd800307.jpg)
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock

Summary of common character sets
随机推荐
128. 最長連續序列-哈希錶
2024. maximum difficulty of the exam - sliding window
Flink传入自定义的参数或配置文件
Background fixing effect
Encapsulate the amount input box component.
Problems that cannot be resolved by tar command
Occupied occupied occupied occupied occupied
MFS详解(四)——MFS管理服务器安装与配置
[sklearn] lightgbm
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Background attribute compound writing
Summary of common character sets
Background color translucent
Regularization to limit the number of digits after the decimal point of an input number
UMI packaging and subcontracting, and compressing to gzip
sql中的Exists用法
2024. 考试的最大困扰度-滑动窗口
Introduction to Chang'an chain node certificate, role and authority management
[character set 9] will GBK be garbled when copied to unicode?
The difference between deep copy and shallow copy











