当前位置:网站首页>650.只有两个键的键盘(动态规划)
650.只有两个键的键盘(动态规划)
2022-07-30 05:39:00 【Linke66】

解题思路:动态规划
对于一个数n,他需要进行多少次的复制全部和粘贴的操作,比如25个A,最大能被25整除(比25小的数中)2的数是5,那么我需要复制5个A粘贴5次就可以得到25个A,那么怎么得到5个A,最大能被5整除(比5小的数中)的数是1,因此只能通过复制一个A并粘贴5次。
假设 n%m=0,n/m=min_val; m的范围[1,n-1];
那么动态转移方程为:
dp[n]=dp[m]+(min_val-1)+1;
dp[m]代表要得到m个A最少需要多少次操作;
min_val-1代表需要对m个A进行粘贴的次数;
最后+1是复制m个A的次数。
class Solution {
public:
int minSteps(int n) {
//找[1,n-1]最大能被n整除的
//16/8=2;那么就是dp[16]=dp[8]+2-1;dp[8]=dp[4]+2-1;dp[4]=dp[2]+2-1;dp[2]=dp[1]+2-1;
vector<int>dp(n+1);
dp[0]=0;
dp[1]=0;
for(int i=2;i<=n;++i){
int min_val=INT_MAX;
int index;
for(int j=i-1;j>=1;--j){
if(i%j==0){
int val=i/j;
if(val<min_val){
min_val=val; //最少的粘贴次数
index=j; //在哪个位置粘贴
}
}
}
dp[i]=dp[index]+min_val-1+1;
}
return dp[n];
}
};
边栏推荐
- 个人博客系统(附源码)
- [Image processing] Image skeleton extraction based on central axis transformation with matlab code
- 相对路径和绝对路径的区别
- flask的笔记
- [Mysql] DATEDIFF function
- Frobenius norm(Frobenius 范数)
- 从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
- MySQL的 DDL和DML和DQL的基本语法
- Machine Learning - Gradient Descent Optimization - C language implementation
- Seata exception: endpoint format should like ip:port
猜你喜欢
![[Other] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)
[Other] DS5

torch.load()

mysql time field is set to current time by default

数据操作 / 数据预处理

MySQL-Explain详解

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution

腾讯面试居然跟我扯了半小时的CountDownLatch

net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.
![[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code](/img/c1/f962f1c1d9f75732157d49a5d1d0d6.png)
[Image detection] Research on cumulative weighted edge detection method based on grayscale image with matlab code
随机推荐
排列数字(DAY90)dfs
cross_val_score的用法
cnpm installation steps
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
mysql 时间字段默认设置为当前时间
ClickHouse data insert, update and delete operations SQL
Difference between cookie and session
Navicat cannot connect to mysql super detailed processing method
解决phpstudy无法启动MySQL服务
[详解C语言]一文带你玩转数组
2022年SQL大厂高频实战面试题(详细解析)
Solve phpstudy unable to start MySQL service
G巴士计数(Google Kickstart2014 Round D Problem B)(DAY 89)
CISP-PTE真题演示
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
微信小程序开发学习
Numpy 中 np.vstack() 和 np.hstack() 简单解析
MySQL-Explain详解
torch.load()
【线性神经网络】线性回归 / 基础优化方法