当前位置:网站首页>剑指 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];
}
};
边栏推荐
- A large select drop-down box, village in Chaoyang District
- SSH登录服务器发送提醒
- How to stand out quickly when you are new to the workplace?
- 低代码平台国际化多语言(i18n)技术方案
- Tensorflow binary installation & Failure
- The upward and downward transformation of polymorphism
- Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
- Xctf mobile--app2 problem solving
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- Analysis of the influence of voltage loop on PFC system performance
猜你喜欢

(最新版) Wifi分销多开版+安装框架

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

有限状态机FSM

Brief introduction to mvcc

2021 autumn Information Security Experiment 1 (password and hiding technology)
![[data mining review questions]](/img/96/00f866135e06c4cc0d765c6e499b29.png)
[data mining review questions]
![[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](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[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

最新版盲盒商城thinkphp+uniapp

如何在微信小程序中获取用户位置?

Togaf certification self-study classic v2.0
随机推荐
It feels great to know you learned something, isn‘t it?
[problem exploration and solution of one or more filters or listeners failing to start]
Swift5.7 扩展 some 到泛型参数
Sqoop1.4.4原生增量导入特性探秘
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Airflow installation jump pit
Deeply understand the mvcc mechanism of MySQL
有限状态机FSM
OpenStack节点地址改变
Xctf mobile--app3 problem solving
Tensorflow binary installation & Failure
强大的头像制作神器微信小程序
Togaf certification self-study classic v2.0
Swift return type is a function of function
Swift Error Handling
Nodejs+Express+MySQL实现登陆功能(含验证码)
How to get user location in wechat applet?
Leetcode234 palindrome linked list
How to stand out quickly when you are new to the workplace?
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'