当前位置:网站首页>剑指 Offer 14- I. 剪绳子
剑指 Offer 14- I. 剪绳子
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 14- I. 剪绳子
给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0]k[1]…*k[m-1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三段,此时得到的最大乘积是18。
示例 1:
输入: 2
输出: 1
解释: 2 = 1 + 1, 1 × 1 = 1
示例 2:
输入: 10
输出: 36
解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36
提示:
2 <= n <= 58
思路
动态规划
递推:a[i]=max(a[i], max( (i-j),a[i-j] ) * max( j,a[j] ));
- max(之前的值, 当前更新的值)
- 当前更新的值=[i-j]范围的最大值 * [0-j]范围最大值
代码
class Solution {
public:
int cuttingRope(int n) {
vector<int> a(60);
a[0]=0;
a[1]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<i;j++){
a[i]=max(a[i],max( (i-j),a[i-j] )*max( j,a[j] ));
}
// printf("%d--%d\n",i,a[i]);
}
return a[n];
}
};
边栏推荐
- Xctf mobile--app1 problem solving
- Sqoop1.4.4原生增量导入特性探秘
- The latest version of blind box mall thinkphp+uniapp
- T430 toss and install OS majave 10.14
- Tensorflow binary installation & Failure
- 有限状态机FSM
- Apache Mina Development Manual
- Huffman coding experiment report
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
- alright alright alright
猜你喜欢
Ali & ant self developed IDE
Powerful avatar making artifact wechat applet
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
initial、inherit、unset、revert和all的区别
[problem exploration and solution of one or more filters or listeners failing to start]
T430 toss and install OS majave 10.14
Xctf mobile--app3 problem solving
Record your vulnhub breakthrough record
2021 autumn Information Security Experiment 1 (password and hiding technology)
随机推荐
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Swift5.7 扩展 some 到泛型参数
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
The best shortcut is no shortcut
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
Create a dojo progress bar programmatically: Dojo ProgressBar
Nodejs+Express+MySQL实现登陆功能(含验证码)
I'm too lazy to write more than one character
Solve the problem of VI opening files with ^m at the end
【判断题】【简答题】【数据库原理】
[exercice 7] [principe de la base de données]
Ten workplace rules
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Node.js: express + MySQL的使用
记录自己vulnhub闯关记录
Powerful avatar making artifact wechat applet
Oh my Zsh + TMUX installation
Idea packages the web project into a war package and deploys it to the server to run
[Exercice 5] [principe de la base de données]