当前位置:网站首页>[leetcode] dynamic programming solution partition array ii[arctic fox]
[leetcode] dynamic programming solution partition array ii[arctic fox]
2022-06-27 21:50:00 【A Fei algorithm】
Their thinking :
Write this question , From an official description I saw “「 Split the array into m paragraph , seek ……」 Dynamic programming is a common question .”, It reminds me of what I wrote before 1043. Separate the arrays to get the maximum and , An early solution to the problem , Style is more like a draft , However, there are still many friends who like to read and praise , Thank you thank you , Refill ~

{:width=“350px”}{:align=“left”}
Define the State
d p [ i ] dp[i] dp[i]: An array of former i i i The number is n u m s [ 0 , 1... i − 1 ] nums[0,1...i-1] nums[0,1...i−1], It's cut Y − 1 Y-1 Y−1 The knife , Divided into Y Y Y An array , The maximum number of arrays shall not exceed K K K, The value of each array becomes the maximum value , The maximum sum after segmentation , Pictured above , When divided into Y = 3 Y=3 Y=3 When there are two parts , The maximum value of the first part is 15 15 15, The second part is 9 9 9, The third part 10 10 10, Each value of each part rises to a local value of the current part m a x max max, The red font is the new value , After accumulation , Find the maximum
Transfer equation
{:width=“450px”}{:align=“left”}
Want to ask d p [ i ] dp[i] dp[i], This is a An array of former i i i The number is n u m s [ 0 , 1... i − 1 ] nums[0,1...i-1] nums[0,1...i−1], It's cut Y − 1 Y-1 Y−1 The knife , Divided into Y Y Y An array , The maximum number of arrays shall not exceed K K K, The value of each array becomes the maximum value , The maximum sum after segmentation
- seek d p [ i − 1 ] dp[i-1] dp[i−1], Represents the front of an array i i i The number is n u m s [ 0 , 1... i − 2 ] nums[0,1...i-2] nums[0,1...i−2], The second part is n u m s [ i − 1 ] nums[i-1] nums[i−1], in other words d p [ i − 1 ] dp[i-1] dp[i−1] + m a x ( n u m s [ i − 1 ] ) max(nums[i-1]) max(nums[i−1])* ( i − ( i − 1 ) ) (i-(i-1)) (i−(i−1))
- seek d p [ i − 2 ] dp[i-2] dp[i−2], Represents the front of an array i − 1 i-1 i−1 The number is n u m s [ 0 , 1... i − 3 ] nums[0,1...i-3] nums[0,1...i−3], The second part is n u m s [ i − 2... i − 1 ] nums[i-2...i-1] nums[i−2...i−1], in other words d p [ i − 2 ] dp[i-2] dp[i−2] + m a x ( n u m s [ i − 2... i − 1 ] ) max(nums[i-2...i-1]) max(nums[i−2...i−1]) * ( i − ( i − 2 ) ) (i-(i-2)) (i−(i−2))
- seek d p [ i − 3 ] dp[i-3] dp[i−3], Represents the front of an array i − 2 i-2 i−2 The number is n u m s [ 0 , 1... i − 4 ] nums[0,1...i-4] nums[0,1...i−4], The second part is n u m s [ i − 3... i − 1 ] nums[i-3...i-1] nums[i−3...i−1], in other words d p [ i − 3 ] dp[i-3] dp[i−3] + m a x ( n u m s [ i − 3... i − 1 ] ) max(nums[i-3...i-1]) max(nums[i−3...i−1]) * ( i − ( i − 3 ) ) (i-(i-3)) (i−(i−3))
- …
- seek d p [ 0 ] dp[0] dp[0], Represents the front of an array 1 1 1 The number is n u m s [ 0 , 0 ] nums[0,0] nums[0,0], The second part is n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1], in other words d p [ 0 ] dp[0] dp[0] + m a x ( n u m s [ 0... i − 1 ] ) max(nums[0...i-1]) max(nums[0...i−1]) * ( i − ( 0 ) ) (i-(0)) (i−(0))
Find the maximum of the above
Can be derived d p [ i ] dp[i] dp[i]= m a x max max( d p [ i ] dp[i] dp[i], d p [ j ] + ( i − j ) ∗ M A X dp[j]+(i-j)*MAX dp[j]+(i−j)∗MAX), among M A X MAX MAX yes n u m s [ j . . . i − 1 ] nums[j...i-1] nums[j...i−1] Local maximum in the range , Once the maximum value is found , All values in this range are changed to this local maximum M A X MAX MAX, among 0=< j j j< i i i
Initialize boundary
i − j i-j i−j If it is greater than K K K, Back n u m s [ i . . . n ] nums[i...n] nums[i...n] There will be no way to cover this part , A condition : i − j i-j i−j<= K K K
j j j>=0, There's nothing to say about this
d p dp dp During initialization, the capacity is n + 1 n+1 n+1, Required d p [ n ] dp[n] dp[n] Express An array of former n n n The number is n u m s [ 0 , 1... n ] nums[0,1...n] nums[0,1...n], It's cut Y − 1 Y-1 Y−1 The knife , Divided into Y Y Y An array , The maximum number of arrays shall not exceed K K K, The value of each array becomes the maximum value , The maximum sum after segmentation
Coding techniques
- i i i Traverse from left to right , j j j Starts i − 1 i-1 i−1 Traverse from right to left
- Note the local maximum M A X MAX MAX
Complete code
public int maxSumAfterPartitioning(int[] A, int K) {
int n = A.length;
int[] dp = new int[n + 1];
for (int i = 0; i <= n; i++) {
int j = i - 1;
int max = dp[i];
while ((i - j) <= K && j >= 0) {
max = Math.max(max, A[j]);
dp[i] = Math.max(dp[i], dp[j] + (i - j) * max);
j--;
}
}
return dp[n];
}
class Solution {
public:
int maxSumAfterPartitioning(vector<int>& A, int K) {
// f[i] = max(f[j] + (i-j)*max(A[j..i]))
int n = A.size();
vector<int> f(n+1);
for (int i = 0; i <= n; i++) {
int curMax = 0;
// consider past integers [j...i]
for (int j = i-1; (i-j)<=K && j>=0; j--) {
curMax = max(curMax, A[j]);
f[i] = max(f[i], f[j] + (i-j)*curMax);
}
}
return f[n];
}
};
summary
- The topic of dynamic planning , We need to think of some basic situations , Think like mathematical induction
边栏推荐
- 豆沙绿保护你的双眼
- 空指针异常
- 强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
- gomock mockgen : unknown embedded interface
- 100 important knowledge points that SQL must master: using functions to process data
- GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
- Acwing周赛57-数字操作-(思维+分解质因数)
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- linux下安装oracle11g 静默安装教程
- [LeetCode]513. 找树左下角的值
猜你喜欢

Go from entry to practice - multiple selection and timeout control (notes)

MYSQL和MongoDB的分析

"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer

How to participate in openharmony code contribution

Go从入门到实战——package(笔记)

Go从入门到实战——依赖管理(笔记)

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

Let Ma Huateng down! Web3.0, hopeless

List of language weaknesses --cwe, a website worth learning

Null pointer exception
随机推荐
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
豆沙绿保护你的双眼
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
vmware虚拟机PE启动
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
Go从入门到实战——接口(笔记)
Go from introduction to actual combat - context and task cancellation (notes)
Go from entry to practice - multiple selection and timeout control (notes)
01-Golang-环境搭建
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
Go 访问GBase 8a 数据库的一个方法
io流代码
集合代码练习
Bit. Store: long bear market, stable stacking products may become the main theme
Go从入门到实战——任务的取消(笔记)
Special training of guessing game
OpenSSL 编程 一:基本概念
Go从入门到实战——共享内存并发机制(笔记)
Go从入门到实战——CSP并发机制(笔记)
数组作业题