当前位置:网站首页>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;
}
}
边栏推荐
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- How to build an interface automation testing framework?
- MNIST implementation using pytoch in jupyter notebook
- CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
- Complete web login process through filter
- Several errors encountered when installing opencv
- MySQL29-数据库其它调优策略
- Ueeditor internationalization configuration, supporting Chinese and English switching
- 软件测试工程师发展规划路线
- Use xtrabackup for MySQL database physical backup
猜你喜欢
Super detailed steps for pushing wechat official account H5 messages
What is the current situation of the game industry in the Internet world?
MySQL21-用户与权限管理
实现微信公众号H5消息推送的超级详细步骤
MySQL32-锁
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
Typescript入门教程(B站黑马程序员)
What should the redis cluster solution do? What are the plans?
Implement sending post request with form data parameter
In fact, the implementation of current limiting is not complicated
随机推荐
MySQL的存储引擎
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
软件测试工程师必备之软技能:结构化思维
Super detailed steps to implement Wechat public number H5 Message push
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
MySQL25-索引的创建与设计原则
MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
MySQL36-数据库备份与恢复
[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in
How to build an interface automation testing framework?
Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
基于Pytorch的LSTM实战160万条评论情感分类
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
Nanny hand-in-hand teaches you to write Gobang in C language
MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
Advantages and disadvantages of evaluation methods
MySQL24-索引的数据结构
Sed text processing
If someone asks you about the consistency of database cache, send this article directly to him
ByteTrack: Multi-Object Tracking by Associating Every Detection Box 论文阅读笔记()