当前位置:网站首页>剑指 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];
}
};
边栏推荐
- 【習題五】【數據庫原理】
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
- Low code platform international multilingual (I18N) technical solution
- Ali & ant self developed IDE
- SSH登录服务器发送提醒
- Approve iPad, which wants to use your icloud account
- Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
- Project video based on Linu development
- Ten workplace rules
- Day 1 of kotlin learning: simple built-in types of kotlin
猜你喜欢

Leetcode234 palindrome linked list
![[problem exploration and solution of one or more filters or listeners failing to start]](/img/82/e7730d289c4c1c4800b520c58d975a.jpg)
[problem exploration and solution of one or more filters or listeners failing to start]

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

2021 autumn Information Security Experiment 1 (password and hiding technology)

Quick learning 1.8 front and rear interfaces

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board

Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward

【数据挖掘复习题】
![[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)

Analysis of the influence of voltage loop on PFC system performance
随机推荐
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
低代码平台国际化多语言(i18n)技术方案
Loan calculator my pressure is high
基于同步坐标变换的谐波电流检测
Using swift language features, write a pseudo-random number generator casually
Huffman coding experiment report
node的ORM使用-Sequelize
4. 无线体内纳米网:电磁传播模型和传感器部署要点
[exercise 7] [Database Principle]
记录自己vulnhub闯关记录
Ten workplace rules
电压环对 PFC 系统性能影响分析
ImportError: No module named examples. tutorials. mnist
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Differences and connections between final and static
[review questions of database principles]
(latest version) WiFi distribution multi format + installation framework
【Colab】【使用外部数据的7种方法】
idea将web项目打包成war包并部署到服务器上运行