当前位置:网站首页>【动态规划】连续子数组的最大和
【动态规划】连续子数组的最大和
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;
}
}边栏推荐
- Discourse Custom Header Links
- Graphical lower_bound & upper_bound
- 12 磁盘相关命令
- SQALE 是什么
- 测试中的误报和漏报同样的值得反复修正
- 学习DAVID数据库(1)
- Number 16, top posts
- YOLOV5学习笔记(二)——环境安装+运行+训练
- 10. Redis implements likes (Set) and obtains the total number of likes
- CloudCompare & PCL calculate the degree of overlap between two point clouds
猜你喜欢

Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击

5. SAP ABAP OData 服务如何支持 $filter (过滤)操作

编译Hudi

8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)

TCP详解(二)

【编译原理】递归下降语法分析设计原理与实现

mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)

6. Display comments and replies

Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest

Mysql 45讲学习笔记(二十五)MYSQL保证高可用
随机推荐
TCP/IP four-layer model
什么是分布式锁?实现分布式锁的三种方式
【shell基础】判断目录是否为空
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
What is SQALE
想从手工测试转岗自动化测试,需要学习哪些技能?
Addition and Subtraction of Scores in LeetCode Medium Questions
php 网站的多语言设置(IP地址区分国内国外)
Local area network computer hardware information collection tool
Multilingual settings of php website (IP address distinguishes domestic and foreign)
一份高质量的测试用例如何养成?
SQL injection Less47 (error injection) and Less49 (time blind injection)
MultipartFile文件上传
execsnoop 工具
CloudCompare&PCL 计算两个点云之间的重叠度
编译Hudi
LeetCode简单题之两个数组间的距离值
False positives and false negatives in testing are equally worthy of repeated corrections
Thesis framework of the opening report
4. Sensitive word filtering (prefix tree)