当前位置:网站首页>[leetcode] dynamic programming solution partition array i[red fox]
[leetcode] dynamic programming solution partition array i[red fox]
2022-06-27 21:50:00 【A Fei algorithm】
Welcome to 、 give the thumbs-up 、 forward 、 subscribe , Between your hands , My power source .

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 ~

Define the State
d p [ i ] [ j ] dp[i][j] dp[i][j] Before presentation i i i The number is n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1] The number between is divided into j j j paragraph , Made up of j j j The smallest of the maximum values of the respective sums of the subarrays
Transfer equation

Think about the class of dynamic programming , A bit like Taoism Dao begets One , One begets Two , Two begets Three , Our vision-be , From special cases to general cases , Deductive induction
Back to this topic, the same is true , The goal is to seek d p [ i ] [ j ] dp[i][j] dp[i][j], Read this definition again : Before presentation i i i The number is n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1] The number between is divided into j j j paragraph , Made up of j j j The smallest of the maximum values of the respective sums of the subarrays , Can we ask him to be here n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1] Try using scissors , Subtract both ends , Consider in two parts ?
The second paragraph : It can be imagined as the j j j paragraph , As shown in the picture n u m s [ k , k + 1 , . . . . i − 1 ] nums[k,k+1,....i-1] nums[k,k+1,....i−1], Because we take the former i i i Number , Array index from 0 0 0 At the beginning , The sum of this part is obviously s u m ( n u m s [ k , k + 1 , . . . i − 1 ] ) sum(nums[k,k+1,...i-1]) sum(nums[k,k+1,...i−1])
The first paragraph : It can be imagined as before j − 1 j-1 j−1 paragraph , What about this paragraph ? Since it is j − 1 j-1 j−1 paragraph , Actually sum j j j There's no big difference between paragraphs , It can be transformed into d p dp dp, That is to say d p [ k ] [ j − 1 ] dp[k][j-1] dp[k][j−1], Before presentation k k k The number is n u m s [ 0... k − 1 ] nums[0...k-1] nums[0...k−1] The number between is divided into j − 1 j-1 j−1 paragraph , Made up of j − 1 j-1 j−1 The minimum value of the maximum value of the respective sum of the subarrays
The minimum value at the top two ends is d p [ i ] [ j ] dp[i][j] dp[i][j] Result , namely d p [ i ] [ j ] dp[i][j] dp[i][j]= m a x max max( d p [ k ] [ j − 1 ] dp[k][j-1] dp[k][j−1], s u m [ k . . . i − 1 ] sum[k...i-1] sum[k...i−1]), Then this is just one of them , That is to say k k k There are many possibilities , k k k The range can be from 0 0 0 Fetch i − 1 i-1 i−1, exceed i − 1 i-1 i−1 It makes no sense , If k k k> i − 1 i-1 i−1, Before presentation k k k Number , But the goal only considers the former i i i Number
The final transfer equation :
d p [ i ] [ j ] dp[i][j] dp[i][j]= m i n min min[ m a x max max( d p [ k ] [ j − 1 ] , s u m [ k . . . i − 1 ] dp[k][j-1],sum[k...i-1] dp[k][j−1],sum[k...i−1])] , among 0=< k k k<= i − 1 i-1 i−1
The border
- d p [ 0 ] [ 0 ] dp[0][0] dp[0][0] Before presentation 0 The number is divided into 0 paragraph , This makes no logical sense , How does this happen , That is the whole n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1] Is divided into j = 1 j=1 j=1 paragraph , that d p [ k ] [ j − 1 ] dp[k][j-1] dp[k][j−1] It becomes d p [ 0 ] [ 0 ] dp[0][0] dp[0][0], Take the same as s u m [ k . . . i − 1 ] sum[k...i-1] sum[k...i−1] The maximum of , As long as you make d p [ 0 ] [ 0 ] dp[0][0] dp[0][0]=0 Will not affect the final result
- j j j> i i i, It makes no sense , because , front i i i Numbers cannot be divided into more than i i i paragraph , Because a number can only be divided into one group at most , If each array is a separate group , That is to say i i i Group , Can not tell greater than i i i Group situation , In the process of variable traversal , j j j The value of should be m i n ( m , i ) min(m,i) min(m,i), m m m Is the upper limit of the number of partitions given in the title
- The outermost m i n min min If the initial value is 0, Will affect the results , Set up a M A X MAX MAX value , Every time the
Prefixes and auxiliary arrays
Definition p r e f i x [ i ] prefix[i] prefix[i] Express n u m s nums nums Before array i i i The number and , namely n u m s [ 0... i − 1 ] nums[0...i-1] nums[0...i−1] Before and , This prefix and array initialization n + 1 n+1 n+1, p r e f i x [ 0 ] prefix[0] prefix[0] redundancy , Most auxiliary array

Take the example in the figure , Printed d p dp dp as follows
[[0,MAX,MAX],
[MAX,7,MAX],
[MAX,9,7],
[MAX,14,7],
[MAX,24,14],
[MAX,32,18]]
A small function
- Fill in a two-dimensional array , Traversal line , Fill each column
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
Complete code
public int splitArray(int[] nums, int m) {
int n = nums.length;
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
Arrays.fill(dp[i], Integer.MAX_VALUE);
}
int[] prefix = new int[n + 1];
for (int i = 0; i < n; i++) {
prefix[i + 1] = nums[i] + prefix[i];
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= Math.min(m, i); j++) {
for (int k = 0; k < i; k++) {
dp[i][j] = Math.min(dp[i][j], Math.max(dp[k][j - 1], prefix[i] - prefix[k]));
}
}
}
// System.out.println(JSON.toJSONString(dp));
return dp[n][m];
}
Complexity analysis :
- Time complexity : O ( M ∗ N ∗ N ) O(M*N*N) O(M∗N∗N) , among N N N It's an array n u m s nums nums The length of , M M M Is the number of segments to split , Three layers f o r for for l o o p loop loop , k k k Maximum to N N N
- Spatial complexity : O ( M ∗ N ) O(M*N) O(M∗N) d p dp dp Space
边栏推荐
- How to delete "know this picture" on win11 desktop
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- 100 important knowledge points that SQL must master: sorting and retrieving data
- Special tutorial - Captain selection game
- Go从入门到实战—— 多路选择和超时控制(笔记)
- 100 important knowledge points that SQL must master: retrieving data
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- Codeforces Round #722 (Div. 2)
- Go从入门到实战——仅执行一次(笔记)
- Knowledge sorting of exception handling
猜你喜欢

win11桌面出现“了解此图片”如何删除

Codeforces Global Round 14

PCIE知识点-008:PCIE switch的结构

语言弱点列表--CWE,一个值得学习的网站

创建对象时JVM内存结构

100 important knowledge points that SQL must master: filtering data

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

01-Golang-环境搭建

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

我想我要开始写我自己的博客了。
随机推荐
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
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
Day8 ---- 云资讯项目介绍与创建
Go from starting to Real - Interface (note)
Go from introduction to practice -- coordination mechanism (note)
分享|智慧环保-生态文明信息化解决方案(附PDF)
OpenSSL 编程 一:基本概念
[LeetCode]515. 在每个树行中找最大值
Xiao Wang's interview training task
OpenSSL 编程 二:搭建 CA
[LeetCode]572. 另一棵树的子树
Quick excel export
Express e stack - small items in array
微服务之远程调用
Go从入门到实战——任务的取消(笔记)
Acwing周赛57-最长连续子序列-(二分or树状数组)
Null pointer exception
"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer
100 important knowledge points that SQL must master: combining where clauses
How to delete "know this picture" on win11 desktop