当前位置:网站首页>Maximum sum of jz42 continuous subarray (force buckle) (GIF diagram)
Maximum sum of jz42 continuous subarray (force buckle) (GIF diagram)
2022-07-27 22:26:00 【Magic tea】
Catalog
subject :
describe
Enter a length of n Integer array array, One or more consecutive integers in an array form a subarray , The minimum length of the subarray is 1. Find the maximum sum of all subarrays .
Data range :
1 <= n <= 2\times10^51<=n<=2×105
-100 <= a[i] <= 100−100<=a[i]<=100
requirement : The time complexity is O(n)O(n), The space complexity is O(n)O(n)
Advanced : The time complexity is O(n)O(n), The space complexity is O(1)O(1)
Example 1
Input :
[1,-2,3,10,-4,7,2,-5]
Return value :
18
explain :
It can be seen from the analysis , Subarray of input array [3,10,-4,7,2] The maximum sum can be obtained as 18
Their thinking :
The maximum value of continuous addition we require , So let's first clarify one thing , First number + The second number > The second number , Such a continuous number will be the largest . At the same time, we set a number storage maximum , Finally, you can go back
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
vector<int> dp(array.size(), 0);// Open up a and array The same size vector Array dp
dp[0] = array[0];
int maxsum = dp[0];
for(int i = 1; i < array.size(); i++){
// Judge the first number + Whether the second number is greater than the second number itself
dp[i] = max(dp[i - 1] + array[i], array[i]);
// See who is bigger than the sum of the two numbers
maxsum = max(maxsum, dp[i]);
}
return maxsum;
}
};The illustration :

边栏推荐
- Leetcode-309- best time to buy and sell stocks, including freezing period
- 软件测试的就业前景到底怎么样?
- Pytoch distributed training
- Wireshark filter rule notes, with software
- Are Transformers Effective for Time Series Forecasting?| Pit filling
- 基于简化的评分卡、Smote采样和随机森林的信贷违约预测
- Cocos: simple application of ccpdistance function and the function of eyeball rotating in orbit with fingers
- 【图解】三次握手,四次挥手 —— 用心看这一篇就够了
- Relationship between DBM and VPP and Vpeak
- JVM garbage collection garbage collector and common combination parameters
猜你喜欢

matlab 绘制三坐标(轴)图

Interview question: talk about your understanding of AQS

Behind every piece of information you collect, you can't live without TA

Drawing three coordinate (axis) diagram with MATLAB

Deepfake 换脸真假难辨,马斯克分克已伪装成功

redis学习

只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)

Project analysis (what is it training that can't be given)

Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track

七大排序之希尔排序
随机推荐
刚培训完的中级测试工程师如何快速度过试用期
What is modcount in the source code? What's the effect
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
Leetcode 301. delete invalid parentheses
Live broadcast software app development, uniapp scroll view hidden scroll bar
What is the employment prospect of software testing?
SQL injection less26a (Boolean blind injection)
Are Transformers Effective for Time Series Forecasting?|填坑
8000字讲透OBSA原理与应用实践
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
七大排序之直接插入排序
jumpserver学习
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
Learn the use principle and core idea of thread pool from the source code
Simple use of enum
Vs2019 release mode debugging: this expression has side effects and will not be evaluated.
Project analysis (from technology to project and product)
[stonedb fault diagnosis] MDL lock waiting
How to learn object Defineproperty | an article takes you to quickly learn
Principle and application of CMOS transmission gate