当前位置:网站首页>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;
}
}
边栏推荐
- 16 medical registration system_ [order by appointment]
- C miscellaneous lecture continued
- MySQL22-逻辑架构
- Super detailed steps for pushing wechat official account H5 messages
- How to change php INI file supports PDO abstraction layer
- MySQL23-存儲引擎
- Download and installation of QT Creator
- 好博客好资料记录链接
- Emotional classification of 1.6 million comments on LSTM based on pytoch
- 实现微信公众号H5消息推送的超级详细步骤
猜你喜欢

If someone asks you about the consistency of database cache, send this article directly to him

Pytorch RNN actual combat case_ MNIST handwriting font recognition

A necessary soft skill for Software Test Engineers: structured thinking

MySQL底层的逻辑架构

MySQL29-数据库其它调优策略

South China Technology stack cnn+bilstm+attention

ZABBIX introduction and installation

Mysql26 use of performance analysis tools

MySQL30-事务基础知识

How to build an interface automation testing framework?
随机推荐
[Julia] exit notes - Serial
基于Pytorch肺部感染识别案例(采用ResNet网络结构)
Implement sending post request with form data parameter
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
What is the current situation of the game industry in the Internet world?
Adaptive Bezier curve network for real-time end-to-end text recognition
Discriminant model: a discriminant model creation framework log linear model
该不会还有人不懂用C语言写扫雷游戏吧
Solution to the problem of cross domain inaccessibility of Chrome browser
MySQL33-多版本并发控制
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)
如何搭建接口自动化测试框架?
解决在window中远程连接Linux下的MySQL
MySQL storage engine
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
Database middleware_ MYCAT summary
MySQL27-索引優化與查詢優化
实现以form-data参数发送post请求
Const decorated member function problem
Redis集群方案应该怎么做?都有哪些方案?