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


Define the State
d p [ i ] dp[i] dp[i] It means that you will i i i Split into at least 2 After the sum of positive integers , The product of these integers
Transfer equation
First, consider the general situation :
d p [ 0 ] dp[0] dp[0] There's no point , Because the subscript is from 0 Start , This value appears , Set it to 0
d p [ 1 ] dp[1] dp[1] 1 Cannot be split into smaller positive integers again , Set it to 0
d p [ 2 ] dp[2] dp[2],2 There is just one way to dismantle it , Split it into two 1, d p [ 2 ] dp[2] dp[2]=1*1=1
…
d p [ i ] dp[i] dp[i], reflection d p [ i ] dp[i] dp[i], It is conceivable that within its scope , We have already dismantled one j j j, from [ 1... j ] [1...j] [1...j] As a whole 1, See the scenario below 1, The rest is [ j + 1.... i ] [j+1....i] [j+1....i], There are two options
- Continue as a whole , For the whole 2, Its value is i − j i-j i−j, obviously d p [ i ] dp[i] dp[i]= j j jX ( i − j ) (i-j) (i−j)
- Continue splitting , But we don't know how many pieces we can split , But we don't care , The result of splitting is d p [ i − j ] dp[i-j] dp[i−j]( Read the definition of state again ), See the scenario below 2, d p [ i ] dp[i] dp[i]= j j jX d p [ i − j ] dp[i-j] dp[i−j]
- therefore d p [ i ] dp[i] dp[i]= j j jX m a x ( i − j , d p [ i − j ] ) max(i-j,dp[i-j]) max(i−j,dp[i−j])
d p [ n ] dp[n] dp[n], Value to return
The border
As discussed above

Complete code
public int integerBreak(int n) {
int[] dp = new int[n + 1];
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = Math.max(dp[i], j * Math.max(i - j, dp[i - j]));
}
}
return dp[n];
}
边栏推荐
- 100 important knowledge points that SQL must master: creating calculation fields
- Go从入门到实战——Context与任务取消(笔记)
- 富文本 考试 填空题
- Golang 使用正则来匹配出子字符串函数
- GBase 8a OLAP函数group by grouping sets的使用样例
- ∫(0→1) ln(1+x) / (x² + 1) dx
- Go from introduction to actual combat - panic and recover (notes)
- 互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
- Go from introduction to actual combat - all tasks completed (notes)
- JVM memory structure when creating objects
猜你喜欢

SQL必需掌握的100个重要知识点:过滤数据

Go from introduction to actual combat - context and task cancellation (notes)

Tiktok's interest in e-commerce has hit the traffic ceiling?

100 important knowledge points that SQL must master: using functions to process data

Special training of guessing game

Go from entry to practice -- CSP concurrency mechanism (note)

ICML2022 | 可扩展深度高斯马尔可夫随机场

01-Golang-环境搭建
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]

Burp suite遇到的常见问题
随机推荐
Little known MySQL import data
io流代码
Magic POI error in reading excel template file
The difference between scrum and Kanban
Burp suite遇到的常见问题
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
Go 访问GBase 8a 数据库的一个方法
∫(0→1) ln(1+x) / (x ² + 1) dx
[LeetCode]515. 在每个树行中找最大值
How to delete "know this picture" on win11 desktop
CEPH distributed storage
How to participate in openharmony code contribution
Go从入门到实战——仅执行一次(笔记)
跟我一起AQS SOS AQS
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
猜拳游戏专题训练
TreeSet详解
抖音的兴趣电商已经碰到流量天花板?
gomock mockgen : unknown embedded interface
Go from entry to practice - multiple selection and timeout control (notes)