当前位置:网站首页>刷题《剑指Offer》day05
刷题《剑指Offer》day05
2022-07-31 08:11:00 【吃豆人编程】
题目来源:力扣《剑指Offer》第二版
完成时间:2022/07/26
14- I. 剪绳子

我的题解
这题用dp的思路求解即可
class Solution {
public:
int max = 0;
int cuttingRope(int n) {
if(n == 2) return 1;
if(n == 3) return 2;
vector<int> product(n+3);
product[0] = 0;
product[1] = 1;
product[2] = 2;
product[3] = 3;
for(int i = 4;i <= n;i++){
max = 0;
for(int j = 1;j <= i/2;j++) {
int num = product[i - j] * product[j];
if(num > max){
max = num;
}
product[i] = max;
}
}
return product[n];
}
};
14- II. 剪绳子 II

我的题解
注意这道题的范围变了,如果再用动态规划的思路会出现溢出。这道题的思路就是尽量把绳子长度大于5时拆分成3(数学不好,证明略)。
class Solution {
public:
int cuttingRope(int n) {
if(n == 2) return 1;
if(n == 3) return 2;
if(n == 4) return 4;
long result = 1;
while(n > 4){
result = (result * 3) % 1000000007;
n-=3;
}
result = (result * n) % 1000000007;
return (int)result;
}
};
边栏推荐
- 使用MySQL如何查询一年中每月的记录数
- 【Unity】编辑器扩展-03-拓展Inspector视图
- The torch distributed training
- skynet中一条消息从取出到处理完整流程(源码刨析)
- [Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI
- 35-Jenkins-共享库应用
- 【云原生与5G】微服务加持5G核心网
- SQL join table (inner join, left join, right join, cross join, full outer join)
- Cloud server deployment web project
- NK - RTU980 burning bare-metal program
猜你喜欢
随机推荐
【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
Vscode: Project-tree plugin
mysql 数据去重的三种方式[实战]
2022杭电杯超级联赛3
【小程序专栏】总结uniapp开发小程序的开发规范
Practical Bioinformatics 2: Multi-omics data integration and mining
【云原生】微服务之Feign的介绍与使用
【idea 报错】 无效的目标发行版:17 的解决参考
[Yellow ah code] Introduction to MySQL - 3. I use select, the boss directly drives me to take the train home, and I still buy a station ticket
regex bypass
A, MySQL principle of master-slave replication
【C#】说说 C# 9 新特性的实际运用
"The C language games" mine clearance
jupyter notebook初使用
SQLAlchemy使用教程
ScheduledExecutorService - 定时周期执行任务
【黄啊码】MySQL入门—3、我用select ,老板直接赶我坐火车回家去,买的还是站票
哆啦a梦教你页面的转发与重定向
ONES 入选 CFS 财经峰会「2022数字化创新引领奖」
【pytorch记录】pytorch的分布式 torch.distributed.launch 命令在做什么呢

![[转载] Virtual Studio 让系统找到需要的头文件和库](/img/85/909c2ef52bbecb3faf7ed683fee65b.png)

![[MySQL exercises] Chapter 5 · SQL single table query](/img/11/66b4908ed8f253d599942f35bde96a.png)





