当前位置:网站首页>[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];
}
边栏推荐
- 有时间看看ognl表达式
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- win11桌面出现“了解此图片”如何删除
- Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
- SQL必需掌握的100个重要知识点:创建计算字段
- Go從入門到實戰——接口(筆記)
- qt base64加解密
- Special tutorial - Captain selection game
- Codeforces Round #723 (Div. 2)
- [LeetCode]动态规划解拆分整数I[Silver Fox]
猜你喜欢

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!"

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

我想我要开始写我自己的博客了。

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]

Go从入门到实战——协程机制(笔记)

Simulink导出FMU模型文件方法

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

Express e stack - small items in array

洛谷P5706 再分肥宅水
随机推荐
Go从入门到实战——协程机制(笔记)
[LeetCode]161. 相隔为 1 的编辑距离
TreeSet详解
Go from introduction to actual combat - panic and recover (notes)
Codeforces Round #717 (Div. 2)
Express e stack - small items in array
Go从入门到实战——错误机制(笔记)
IO stream code
TypeScript学习
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
微服务之远程调用
Have time to look at ognl expressions
SQL必需掌握的100个重要知识点:使用函数处理数据
OpenSSL 编程 一:基本概念
Tiktok's interest in e-commerce has hit the traffic ceiling?
SQL必需掌握的100个重要知识点:创建计算字段
关于异常处理的知识整理
excel读取文件内容方法
List of language weaknesses --cwe, a website worth learning
Go從入門到實戰——接口(筆記)