当前位置:网站首页>[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
边栏推荐
猜你喜欢
[Compilation principle] Design principle and implementation of recursive descent parsing
StringJoiner in detail
What is distributed and clustered?What is the difference?
【C语言】进制转换一般方法
IDEA 注释报红解决
4. Sensitive word filtering (prefix tree)
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
JS function this context runtime syntax parentheses array IIFE timer delay self.backup context call apply
php 网站的多语言设置(IP地址区分国内国外)
The simulation application of common mode inductance is here, full of dry goods for everyone
随机推荐
【Android】Room —— SQLite的替代品
Annotation usage meaning
【C语言】表达式求值的一般方法
Local area network computer hardware information collection tool
Modbus on AT32 MCUs
SQL injection Less54 (limited number of SQL injection + union injection)
Software accumulation -- Screenshot software ScreenToGif
TCP详解(三)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
web容器及IIS --- 中间件渗透方法1
想从手工测试转岗自动化测试,需要学习哪些技能?
[C language foundation] Solve C language error: expected ';', ',' or ')' before '&' token
观察者模式
[Compilation principle] Lexical analysis program design principle and implementation
编译Hudi
CorelDRAW2022 streamlined Asia Pacific new features in detail
WebSocket Session为null
7、私信列表
LeetCode simple problem to find the subsequence of length K with the largest sum
【编译原理】词法分析程序设计原理与实现