当前位置:网站首页>【动态规划】连续子数组的最大和
【动态规划】连续子数组的最大和
2022-07-31 03:06:00 【#小苏打】
动态规划(DP):动态规划中每一个状态一定是由上一个状态推导出来的,有别于贪心,贪心是没有状态推导,而是从局部中选择最优的。
动态规划解题步骤

1:确定dp数组,dp[ i ]为包括下标 i 之前的最大连续子序列和。
2:确定状态转移方程,dp[ i ]有两个方向推出,dp[ i-1 ]+nums[ i ] 为加入当前连续子序列和,
num[ i ]为从头开始计算到下标为 i 的子序列和。
3:初始化dp数组,本题目可以看出dp[ i ]依赖于dp[ i-1 ],所以dp[ 0 ]是递推公司基础。
4:确定遍历顺序,本题需要从前往后遍历。
class Solution {
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0]; //初始化dp
int max = dp[0]; //作为一个中间变量输出最大值
for(int i = 1;i<nums.length;i++){
dp[i] = Math.max(dp[i-1]+nums[i],nums[i]); //状态转移方程
max = Math.max(max,dp[i]);
}
return max;
}
}边栏推荐
- Chapter 9 SVM实践
- Thesis framework of the opening report
- try-catch中含return
- 【C语言】进制转换一般方法
- The use of font compression artifact font-spider
- MultipartFile file upload
- Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
- LeetCode中等题之分数加减运算
- SQL injection Less54 (limited number of SQL injection + union injection)
- 【HCIP】ISIS
猜你喜欢

LeetCode简单题之找到和最大的长度为 K 的子序列

想从手工测试转岗自动化测试,需要学习哪些技能?

华为分布式存储FusionStorage知识点总结【面试篇】

YOLOV5 study notes (2) - environment installation + operation + training

12 Disk related commands

【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.

10 Permission introduction
![[Android] Room - Alternative to SQLite](/img/52/0bc1c0a3173da6d39224ad8440a462.png)
[Android] Room - Alternative to SQLite

CorelDRAW2022 streamlined Asia Pacific new features in detail

7年经验,功能测试工程师该如何一步步提升自己的能力呢?
随机推荐
PMP微信群日常习题
The use of font compression artifact font-spider
The Map Entry understanding and application
Modbus on AT32 MCUs
SQL注入 Less46(order by后的注入+rand()布尔盲注)
YOLOV5学习笔记(二)——环境安装+运行+训练
Several common errors when using MP
8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
共模电感的仿真应用来了,满满的干货送给大家
品牌广告投放平台的中台化应用与实践
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
YOLOV5学习笔记(三)——网络模块详解
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
开题报告之论文框架
LeetCode简单题之找到和最大的长度为 K 的子序列
冒泡排序、选择排序、直接插入排序、二分法查找
C#远程调试