当前位置:网站首页>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];
}
}边栏推荐
- [jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality
- Interview question -- event cycle
- Query the information of students whose grades are above 80
- 55k is stable, and the recommendation system will always drop God!
- Sum of "n" numbers of force deduction summary
- 10. 509 Certificate (structure + principle)
- Test question f: statistical submatrix
- Reasons for not sending requests after uni app packaging
- JS foundation -- data
- mysql_ Master slave synchronization_ Show slave status details
猜你喜欢

JS written test questions -- random numbers, array de duplication

Tensorflow's study notes (I)
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Color space (1) - RGB

Resolve the error: org.apache.ibatis.binding.bindingexception

How to use two queues to simulate the implementation of a stack
![[stm32f130rct6] idea and code of ultrasonic ranging module](/img/a6/1bae9d5d8628f00acf4738008a0a01.png)
[stm32f130rct6] idea and code of ultrasonic ranging module

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

Hashcode details

New key points of ES6
随机推荐
mysql_ Account authorization permission recycling, account locking and unlocking, account creation and deletion
New key points of ES6
Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
Idea configuration
JS construct binary tree
What is technical support| Daily anecdotes
Eslint error
Reasons for not sending requests after uni app packaging
JS interview question - what is the difference between Es5 and ES6?
DOM operation -- get elements and nodes
Enter an integer and a binary tree
How does Jupiter notebook change themes and font sizes?
How is the virtual DOM different from the actual DOM?
Take a database statement note: when the field is empty, assign the default value to the result
[brother hero July training] day 19: binary tree
Riotboard development board series notes (VIII) -- building desktop system
Tensorflow's study notes (I)
Database transactions (often asked)
Operator explanation - C language
[template engine] microservice Learning Notes 6: freemaker