当前位置:网站首页>剑指 Offer 14- I. 剪绳子
剑指 Offer 14- I. 剪绳子
2022-06-29 23:06:00 【anieoo】
原题链接:剑指 Offer 14- I. 剪绳子

solution:
动态规划:
状态表示:dp[i]表示长度为i的绳子分段后乘积的最大值
长度为i的绳子可以减成 j 和 i - j两段,此时可以分成两种情况:
① i - j 不继续减,因此dp[i] = max(dp[i], j * (i - j));
② i - j 继续减,因此dp[i] = max(dp[i], j * dp[i - j]);
因此得出转移方程:
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]表示长度为i的绳子剪完后各段乘积的最大值
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)); //转移方程
}
}
return dp[n];
}
};贪心:
尽可能多的减长度为3的绳子。
class Solution {
public:
int cuttingRope(int n) {
if(n <= 3) return n - 1; //特殊情况
int res = 1;
while(n > 4) {
res *= 3;
n -= 3;
}
return res * n;
}
};
边栏推荐
- Become the only key
- Cacti关于spine轮询的设置
- constexpr 函数
- 开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 十大券商:“推土机行情”再现
- Speech signal processing (III): speech signal analysis [continuous "analog signal" -- Sampling, quantization, coding -- > discrete "digital signal"]
- Node data collection and remote flooding transmission of label information
- Mysql database: the difference between drop, truncate and delete
- matplotlib matplotlib中plt.hist()参数解释
- 111.简易聊天室14:聊天室客户端
猜你喜欢

Pain points and solutions of M1 notebook home office | community essay solicitation

为什么 JSX 语法这么香?

Speech signal processing (II): phonation physiology, auditory physiology and auditory psychology

语音信号处理(二): 发声生理、听觉生理与听觉心理

开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理

Redis client

sql刷题595. 大的国家

字节云数据库未来方向的探索与实践

语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】
随机推荐
Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages
[learn FPGA programming from scratch -51]: high level chapter - FPGA development based on IP core - what is FPGA IP core (soft core, fixed core, hard core) and learning methods
Speech signal processing (II): phonation physiology, auditory physiology and auditory psychology
Test d'installation du cluster metaq
Gracefully transform the SMS business module and start the strategic mode!
声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
Evaluation of powerful and excellent document management software: image management, book management and document management
C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数
Project 1 - buffer pool [cmu 15-445645] notes
SQL question brushing 595 Big country
Software testing interface testing postman testing tool interface testing process execution interface testing interface associated environment variables and global variables built-in dynamic parameter
[从零开始学习FPGA编程-51]:高阶篇 - 基于IP核的FPGA开发- 什么是FPGA IP核(软核、固核、硬核)与学习方法
Profit distribution and taxation of funds
Cacti最大监控数测试
均值、方差、标准差、协方差的概念及意义
Touch key and key control corresponding LED status reversal
0. grpc environment setup
Sword finger offer 38 Arrangement of strings
Shell -- text processing command
Software testing interface testing JMeter 5.5 installation tutorial