当前位置:网站首页>[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
边栏推荐
猜你喜欢
How to delete "know this picture" on win11 desktop
Go从入门到实战—— 多路选择和超时控制(笔记)
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
Codeforces Global Round 14
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
Set code exercise
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
Go from entry to practice -- CSP concurrency mechanism (note)
随机推荐
Go从入门到实战——多态(笔记)
GBase 8a OLAP分析函数cume_dist的使用样例
ICML2022 | 可扩展深度高斯马尔可夫随机场
100 important knowledge points that SQL must master: retrieving data
快速excel导出
熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app
oracle迁移mysql唯一索引大小写不区分别怕
PCIE知识点-008:PCIE switch的结构
Codeforces Round #717 (Div. 2)
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
How to delete "know this picture" on win11 desktop
[Sword Offer II]剑指 Offer II 029. 排序的循环链表
让马化腾失望了!Web3.0,毫无希望
Go从入门到实战——package(笔记)
Set code exercise
Educational Codeforces Round 108 (Rated for Div. 2)
SQL必需掌握的100个重要知识点:用通配符进行过滤
∫(0→1) ln(1+x) / (x ² + 1) dx
鲜为人知的mysql导入数据