当前位置:网站首页>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;
}
}
边栏推荐
- MySQL34-其他数据库日志
- ZABBIX introduction and installation
- MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
- [Julia] exit notes - Serial
- C miscellaneous shallow copy and deep copy
- 该不会还有人不懂用C语言写扫雷游戏吧
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- Download and installation of QT Creator
- Several errors encountered when installing opencv
- Mysql28 database design specification
猜你喜欢
随机推荐
Database middleware_ MYCAT summary
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
ZABBIX introduction and installation
Mysql23 storage engine
MySQL32-锁
How to make shell script executable
How to find the number of daffodils with simple and rough methods in C language
Software test engineer development planning route
First blog
Pytorch RNN actual combat case_ MNIST handwriting font recognition
What is the difference between TCP and UDP?
Mysql24 index data structure
Use JUnit unit test & transaction usage
Anaconda3 安装cv2
MySQL combat optimization expert 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
MySQL34-其他数据库日志
Mysql35 master slave replication
Download and installation of QT Creator
[Julia] exit notes - Serial
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计









