当前位置:网站首页>[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];
}
边栏推荐
- 神奇的POI读取excel模板文件报错
- 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!"
- 微服务之远程调用
- TreeSet详解
- 100 important knowledge points that SQL must master: sorting and retrieving data
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- CORBA 架构体系指南(通用对象请求代理体系架构)
- 100 important knowledge points that SQL must master: using functions to process data
- Go from introduction to actual combat - package (notes)
- oracle迁移mysql唯一索引大小写不区分别怕
猜你喜欢

Full record of 2022 open source moment at Huawei partners and Developers Conference

Codeforces Round #717 (Div. 2)

猜拳游戏专题训练

Let Ma Huateng down! Web3.0, hopeless

关于异常处理的知识整理

win11桌面出現“了解此圖片”如何删除

Go从入门到实战——依赖管理(笔记)

流程控制任务

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

Bit. Store: long bear market, stable stacking products may become the main theme
随机推荐
Go 访问GBase 8a 数据库的一个方法
快速excel导出
GBase 8a数据库用户密码安全相关参数汇总
qt base64加解密
Go from starting to Real - Interface (note)
win11桌面出现“了解此图片”如何删除
Go从入门到实战——接口(笔记)
Go從入門到實戰——接口(筆記)
Special tutorial - Captain selection game
oracle迁移mysql唯一索引大小写不区分别怕
Bit. Store: long bear market, stable stacking products may become the main theme
SQL必需掌握的100个重要知识点:过滤数据
Acwing周赛57-数字操作-(思维+分解质因数)
Is it safe to open an account and buy stocks? Who knows
Go从入门到实战——package(笔记)
Method of reading file contents by Excel
富文本 考试 填空题
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
Go从入门到实战——Panic和recover(笔记)