当前位置:网站首页>剑指 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];
}
};
边栏推荐
- Four problems and isolation level of MySQL concurrency
- CVPR 2022 图像恢复论文
- Dojo tutorials:getting started with deferrals source code and example execution summary
- How to get user location in wechat applet?
- 十条职场规则
- Express abstract classes and methods
- Nodejs+Express+MySQL实现登陆功能(含验证码)
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
- Low code platform international multilingual (I18N) technical solution
- 低代码平台国际化多语言(i18n)技术方案
猜你喜欢

【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】

Attack and defense world mobile--ph0en1x-100

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

Node. Js: use of express + MySQL

idea将web项目打包成war包并部署到服务器上运行

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
![[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)

Four problems and isolation level of MySQL concurrency
随机推荐
自抗扰控制器七-二阶 LADRC-PLL 结构设计
Express abstract classes and methods
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
[exercice 7] [principe de la base de données]
Alibaba is bigger than sending SMS (user microservice - message microservice)
node的ORM使用-Sequelize
Approve iPad, which wants to use your icloud account
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
十条职场规则
Keep learning swift
[comprehensive question] [Database Principle]
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
基于同步坐标变换的谐波电流检测
Using swift language features, write a pseudo-random number generator casually
Simple use and precautions of kotlin's array array and set list
Quick learning 1.8 front and rear interfaces
最新版盲盒商城thinkphp+uniapp
【习题六】【数据库原理】
【習題五】【數據庫原理】
Create a dojo progress bar programmatically: Dojo ProgressBar