当前位置:网站首页>刷题《剑指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;
}
};
边栏推荐
- [Cloud native] Introduction and use of Feign of microservices
- 【Unity】编辑器扩展-03-拓展Inspector视图
- SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
- mysql insert new field method
- 【pytorch记录】pytorch的分布式 torch.distributed.launch 命令在做什么呢
- 会话技术之Coookie && Session详解
- Failure scenarios of @Transactional annotations
- 基于golang的swagger超贴心、超详细使用指南【有很多坑】
- 哆啦a梦教你页面的转发与重定向
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
猜你喜欢

射频电路学习之滤波电路

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

【idea 报错】 无效的目标发行版:17 的解决参考

SSM整合案例分析(详解)

C语言三子棋(井字棋)小游戏

The torch distributed training

SSM integration case study (detailed)

First acquaintance with NK-RTU980 development board

奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
![[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko](/img/11/66b4908ed8f253d599942f35bde96a.png)
[MySQL exercises] Chapter 4 · Explore operators in MySQL with kiko
随机推荐
功能强大的国产Api管理工具
动态顺序表的增删查改(C语言实现)
循环结构--for循环
MySQL 5.7 安装教程(全步骤、保姆级教程)
skynet中一条消息从取出到处理完整流程(源码刨析)
我的创作纪念日
MySQL 数据库基础知识(系统化一篇入门)
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
0730~Mysql优化
求职产品经理【九】求职季,如何写好一份简历?
二维坐标工具API
MySQL中InnoDB的多版本并发控制(MVCC)的实现
Ubuntu22.04安装mysql
How to upgrade nodejs version
Which strings will be parsed as null by FastJson?
【云原生&微服务五】Ribbon负载均衡策略之随机ThreadLocalRandom
"C language" frog jumping steps recursion problem
google搜索技巧——程序员推荐
Ceph single node deployment
Vscode:Project-tree插件