当前位置:网站首页>Three line code solution - Maximum sub segment and - DP
Three line code solution - Maximum sub segment and - DP
2022-06-12 01:25:00 【Doghead Intern】
Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and .
Subarray Is a continuous part of the array .
Example 1:
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 .
Example 2:
Input :nums = [1]
Output :1
Example 3:
Input :nums = [5,4,-1,7,8]
Output :23
Tips :
1 <= nums.length <= 105
-104 <= nums[i] <= 104
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-subarray
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Write down the solution , The first n The maximum sum of terms is equal to n Xiangjiadi n-1 Item maximum and .
In layman's terms :
Let's nums = [-2,1,-3,4,-1,2,1,-5,4] As a sub problem :
The first sub question : The maximum value of the first term max(nums[0])
The second sub problem : The maximum value from the first term to the second term is max(nums[0])+nums[1]
........
The first n The sub problem of : From item one to item two n The maximum value of the item max(nums[n-1])+nums[n]
We can know , If n-1 Item maximum is greater than zero , that max(nums[n-1])+nums[n] It is bound to be greater than nums[n]
Empathy , If n-1 The maximum value of the item is less than or equal to zero , that max(nums[n-1])+nums[n] Must be less than or equal to nums[n]
So we can deduce an equation :
max_nums[n] = max_muns[n-1]+nums[n] if(max_muns[n-1]>0)
nums[n] if(max_muns[n-1]<=0)
We just need to compare max and nums[n], Assign a large value to max that will do
I wrote it on the Internet java Code .
Here is AC Code
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
for(int i = 1;i<nums.length;i++){
nums[i] = nums[i-1]>0?nums[i]+nums[i-1]:nums[i];
max = nums[i]>max?nums[i]:max;
}
return max;
}
}边栏推荐
- Detailed explanation and examples of common parameters of curl
- jvm: 线程上下文类加载器(TheadContextClassLoader)
- Inventory: more than 20 typical safety incidents occurred in February, with a loss of nearly $400million
- In depth description of Weibull distribution (1) principle and formula
- State Administration of market supervision and state Internet Information Office: carry out data security management certification
- 【项目实训】微信公众号获取用户openid
- Component introduction - large screen cloud minimalist user manual
- pca从0到1
- 100 deep learning cases | day 41: speech recognition - pytorch implementation
- [project training] wechat official account template message push
猜你喜欢

Set up NFT blind box mall system | customized development of NFT mall software
![[project training] wechat official account to obtain user openid](/img/54/0a77e4441ee87e6ff04f3f07baa965.png)
[project training] wechat official account to obtain user openid

Codemirror 2 - highlight only (no editor) - codemirror 2 - highlight only (no editor)

System. Commandline option

新知识:Monkey 改进版之 App Crawler

New knowledge: monkey improved app crawler

In depth description of Weibull distribution (2) meaning of parameters and formulas

Weekly CTF 第一周:神奇的磁带

Kill, pkill, killall, next, what I brought to you in the last issue is how to end the process number and rush!

The CSV used for JMeter performance test is bullshit
随机推荐
Program environment and pretreatment
Practice of Flink CDC + Hudi massive data entering the lake in SF
打造Flutter高性能富文本编辑器——渲染篇
The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?
The CSV used for JMeter performance test is bullshit
[n32g457] remote monitoring of graffiti cloud based on RT thread and n32g457
Peewee module basic use -orm
[project training] wechat official account to obtain user openid
一文get,最容易碰上的接口自动化测试问题汇总
I'm fired because I can only test basic functions····
Global and Chinese medical styrene block copolymer industry prospect research and investment planning proposal report 2022-2028
我在某大厂做软件测试工程师的《一天完整工作流程》
Recursive and non recursive transformation
Explain asynchronous tasks in detail: the task of function calculation triggers de duplication
2022-06-11:注意本文件中,graph不是邻接矩阵的含义,而是一个二部图。 在长度为N的邻接矩阵matrix中,所有的点有N个,matrix[i][j]表示点i到点j的距离或者权重, 而在二部
Industry competition analysis and investment scale research report of global and Chinese micro potato industry 2022-2028
SQL exercise summary 3
Interpretation of the guiding opinions on the digital transformation of banking and insurance industry by Analysys analysis
Analysis report on demand status and Prospect Forecast of global and Chinese remote control helicopter industry 2022-2028
PCA from 0 to 1