当前位置:网站首页>Li Kou 343 integer partition dynamic programming
Li Kou 343 integer partition dynamic programming
2022-07-25 03:19:00 【Tension √】
Title Description
Given a positive integer n , Divide it into k individual Positive integer And ( k >= 2 ), And maximize the product of these integers .
return The maximum product you can get .
Their thinking
The six steps of using dynamic programming to solve problems :
- determine dp Array and the meaning of array subscript (dp The array may be one-dimensional or two-dimensional );
- According to the title , Will define arr [ i ] Is a positive integer i After splitting into the sum of at least two positive integers , The maximum product of these positive integers ;
- Determine the recurrence formula ( State transition equation );
- Suppose we start with a positive integer
i (i>2)Take out a positive integerj (1<= j < i), Is equivalent toiSplit intojandi-jTwo parts , Then there are two options ( state ):i-j Part no longer split , Then at this time, integer i The split product is j * (i-j)
i-j Part no longer split , Then at this time, integer i The split product is j * arr(i-j)
therefore , It can be concluded that the recurrence formula is arr[i] = max{max(j*(i-j), j*arr(i-j))}, among 1<= j < i, That's traversal j, Calculate in each cycle max(j*(i-j), j*arr(i-j)), After the final traversal , take max(j*(i-j), j*arr(i-j)) The maximum value of is taken as arr[i] The value of .
- Suppose we start with a positive integer
- initialization dp Array ;
- Array initialization : Subject requirements n>=2, therefore , about 0 and 1 We assign it as 0 that will do ;
- determine dp The traversal order of the array , May be forward traversal 、 Reverse traversal 、 Obliquely ergodic ;
- Give an example to deduce dp Array , Ensure the accuracy of derivation ;
I / O example

Code
class Solution {
public int integerBreak(int n) {
if(n == 1)return 0;
int[] arr = new int[n + 1];
arr[0] = arr[1] = 0;
for(int i = 2; i <= n; i++){
int maxmultip = 0;
for(int j = 1; j < i; j++){
maxmultip = Math.max(maxmultip,Math.max(j*(i - j),j*(arr[i - j])));
}
arr[i] = maxmultip;
}
return arr[n];
}
}边栏推荐
- 05 - MVVM model
- Direct insert sort / Hill sort
- CVPR 2020 | social stgcnn: pedestrian trajectory prediction based on graph convolution
- The dolphin scheduler calls the shell script and passes multiple parameters
- Backtracking to solve subset problem
- # CF #807 Div.2(A - D)
- Riotboard development board series notes (VIII) -- building desktop system
- Banana pie bpi-m5 toss record (3) -- compile BSP
- Import and export using poi
- PHP record
猜你喜欢
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Preliminary foundation JVM

Error: tomee required to support ear/ejb deployment

Introduction to Apache Doris grafana monitoring indicators

Win10 -- open the hosts file as an administrator

Vscode configuration, eslint+prettier combined with detailed configuration steps, standardized development

Dc-2-range practice

Banana pie bpi-m5 toss record (3) -- compile BSP

Record once C # extract audio files with ffmempeg

Swagger key configuration items
随机推荐
Openlayers ol ext: Transform object, rotate, stretch, zoom in
JS construction linked list
Experiment 4 CTF practice
Learning Record V
Error: tomee required to support ear/ejb deployment
Take a database statement note: when the field is empty, assign the default value to the result
Calculation method of confusion matrix
Backtracking to solve subset problem
Resolve the error: org.apache.ibatis.binding.bindingexception
JS interview question - what is the difference between Es5 and ES6?
[jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality
C language writes a circular advertising lantern or changes it to a confession system
How chemical enterprises choose digital service providers with dual prevention mechanism
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
Riotboard development board series notes (4) -- using Vpu hardware decoding
Edit mathematical formulas in markdown
From input URL to page presentation
Enter an integer and a binary tree
New key points of ES6
Riotboard development board series notes (6) -- buildreoot building system image