当前位置:网站首页>[Dynamic programming] Maximum sum of consecutive subarrays
[Dynamic programming] Maximum sum of consecutive subarrays
2022-07-31 03:10:00 【#baking soda】
Dynamic programming (DP): Each state in dynamic programming must be derived from the previous state, which is different from greed, which has no state derivation, but selects the optimal from the local.
Dynamic programming problem solving steps
1: Determine the dp array, dp[ i ] is the maximum consecutive subsequence sum before subscript i.
2: Determine the state transition equation, dp[ i ] has two directions, dp[ i-1 ]+nums[ i ] is adding the current continuous subsequence sum,
num[ i ] is the sum of subsequences to index i calculated from scratch.
3: Initialize dp array, this topic can see that dp[ i ] depends on dp[ i-1 ], so dp[ 0 ] is the basis of recursive company.
4: Determine the traversal order, this question needs to be traversed from front to back.
class Solution {public int maxSubArray(int[] nums) {int[] dp = new int[nums.length];dp[0] = nums[0]; //Initialize dpint max = dp[0]; // output the maximum value as an intermediate variablefor(int i = 1;i
边栏推荐
猜你喜欢
STM32 problem collection
Chapter 9 SVM Practice
11、Redis实现关注、取消关注以及关注和粉丝列表
LeetCode中等题之分数加减运算
4. Sensitive word filtering (prefix tree)
Local area network computer hardware information collection tool
php 网站的多语言设置(IP地址区分国内国外)
YOLOV5学习笔记(三)——网络模块详解
Problems that need to be solved in distributed system architecture
【C语言】进制转换一般方法
随机推荐
【HCIP】ISIS
Crypto Firms Offer Offer To Theft Hackers: Keep A Little, Give The Rest
如何搭建私有yum源
Discourse Custom Header Links
Discourse 自定义头部链接(Custom Header Links)
刚出道“一战成名”,安全、舒适一个不落
Installation of mysql5.7.37 under CentOS7 [perfect solution]
MultipartFile file upload
Observer pattern
【Cocos Creator 3.5】缓动系统停止所有动画
els 方块向右移动边界判断、向下加速
华为分布式存储FusionStorage知识点总结【面试篇】
Number 16, top posts
LeetCode simple problem to find the subsequence of length K with the largest sum
execsnoop 工具
LeetCode简单题之两个数组间的距离值
Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
PMP微信群日常习题
【C语言】进制转换一般方法