当前位置:网站首页>February 13, 2022 - Maximum subarray and
February 13, 2022 - Maximum subarray and
2022-07-06 10:36:00 【Procedural ape does not lose hair 2】
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 <= 10^5
-10^4 <= nums[i] <= 10^4
java Code :
class Solution {
public int maxSubArray(int[] nums) {
// int pre = 0, maxAns = nums[0];
// for (int x : nums) {
// pre = Math.max(pre + x, x);
// maxAns = Math.max(maxAns, pre);
// }
// return maxAns;
// Dynamic programming , Record dp[i] Record from 0 To i The largest substring of
int[] dp = new int[nums.length];
dp[0] = nums[0];
int maxAns = nums[0];
for(int i=1;i<nums.length;i++) {
dp [i] = Math.max(nums[i], dp[i-1]+nums[i]);
maxAns = Math.max(maxAns, dp[i]);
}
return maxAns;
}
}
边栏推荐
- Jar runs with error no main manifest attribute
- CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
- Moteur de stockage mysql23
- Redis集群方案应该怎么做?都有哪些方案?
- Mysql36 database backup and recovery
- Advantages and disadvantages of evaluation methods
- 软件测试工程师发展规划路线
- Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
- Export virtual machines from esxi 6.7 using OVF tool
- How to find the number of daffodils with simple and rough methods in C language
猜你喜欢

MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine

Pytoch LSTM implementation process (visual version)

Moteur de stockage mysql23

MySQL24-索引的数据结构

Complete web login process through filter

使用OVF Tool工具从Esxi 6.7中导出虚拟机

MySQL的存储引擎

Const decorated member function problem

解决在window中远程连接Linux下的MySQL

What should the redis cluster solution do? What are the plans?
随机推荐
MySQL26-性能分析工具的使用
Record the first JDBC
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
Implement sending post request with form data parameter
The underlying logical architecture of MySQL
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
MySQL25-索引的创建与设计原则
Typescript入门教程(B站黑马程序员)
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
Good blog good material record link
MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
Sed text processing
How to make shell script executable
How to change php INI file supports PDO abstraction layer
MySQL的存储引擎
Mysql27 - Optimisation des index et des requêtes
[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in