当前位置:网站首页>Sword finger offer 14- I. cut rope
Sword finger offer 14- I. cut rope
2022-06-29 23:39:00 【anieoo】
Original link : The finger of the sword Offer 14- I. Cut the rope

solution:
Dynamic programming :
State means :dp[i] The length is i The maximum value of the product after the rope is segmented
The length is i The rope of can be reduced to j and i - j Two paragraphs , This can be divided into two cases :
① i - j No further reduction , therefore dp[i] = max(dp[i], j * (i - j));
② i - j Continue to reduce , therefore dp[i] = max(dp[i], j * dp[i - j]);
So it is concluded that Transfer equation :
dp[i] = max(dp[i], j * max(dp[i - j], i - j));
class Solution {
public:
int cuttingRope(int n) {
if(n == 2) return 1;
if(n == 3) return 2;
vector<int> dp(n + 1); //dp[i] The length is i The maximum value of the product of each section after the rope is cut
for(int i = 1;i <= n;i++) {
for(int j = 1;j < i;j++) {
dp[i] = max(dp[i], j * max(dp[i - j], i - j)); // Transfer equation
}
}
return dp[n];
}
};greedy :
Reduce the length as much as possible to 3 The rope of .
class Solution {
public:
int cuttingRope(int n) {
if(n <= 3) return n - 1; // A special case
int res = 1;
while(n > 4) {
res *= 3;
n -= 3;
}
return res * n;
}
};
边栏推荐
- Sword finger offer 38 Arrangement of strings
- shell-运算符
- Solr基础操作2
- Speech signal processing (II): phonation physiology, auditory physiology and auditory psychology
- 2022 PMP project management examination agile knowledge points (5)
- matplotlib matplotlib中plt.hist()参数解释
- 软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
- Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
- 二叉树的序列化 力扣 297. 二叉树的序列化与反序列化 652. 寻找重复的子树
- Create an API rapid development platform, awesome!
猜你喜欢

按头安利 好看又实用的点胶机 SolidWorks模型素材看这里

Leetcode 1385. Distance value between two arrays

Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages

“微博评论”的高性能高可用计算架构

Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here

打造一个 API 快速开发平台,牛逼!

Paper writing tool: latex online website

LC: maximum subarray and

2022 PMP project management examination agile knowledge points (5)

CE第二次作业
随机推荐
请指教什么是在线开户?另外,手机开户安全么?
AI赋能新零售,「智」胜之道在于生态思维|数智夜话直播精选摘录
剑指 Offer 13. 机器人的运动范围
Speech signal processing (III): speech signal analysis [continuous "analog signal" -- Sampling, quantization, coding -- > discrete "digital signal"]
招商证券靠谱吗?开股票账户安全吗?
Cacti关于spine轮询的设置
CE第二次作业
为什么 JSX 语法这么香?
Pytest initializing and cleaning up the environment
LC:最大子数组和
Is it safe to open a stock account? Shanghai stock account opening.
@Scheduled annotation pit, I stepped on it for you
Provide effective performance evaluation 
LC:有效的数独 + 旋转图像
开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
C pointer advanced 2-- > function pointer array callback function simplifies calculator code, and implements qsort function based on callback function simulation
redis客户端
Principe de réalisation de l'agent dynamique
[LeetCode] 只出现一次的数字【136】