650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
Copy All
: You can copy all the characters present on the notepad (partial copy is not allowed).Paste
: You can paste the characters which are copied last time.
Given a number n
. You have to get exactly n
'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n
'A'.
Example 1:
Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'.
题目含义:求获取n个A的最小操作步骤的数目minStep
Copy All
: 只能全部复制,不能部分复制.Paste
: 拷贝留在剪贴板上的字符
1 public int minSteps(int n) { 2 // 先把dp的最小步骤都设置为无穷大, 初始化条件为:dp[0]=dp[1]=0 ,状态转移方程为: 3 // dp[i]=min(dp[i],dp[j]+i/j),i>1,j<i且i是j的整数倍 4 // 上述状态转移方程表示:如果i是j的倍数,那么i可以通过粘贴(i/j-1)次j 得到, 再加上一次复制操作, 5 int[] dp = new int[n+1]; 6 if (n<2) return 0; 7 Arrays.fill(dp,Integer.MAX_VALUE); 8 dp[0]=0; 9 dp[1]=0; 10 for (int i=2;i<n+1;i++) 11 { 12 for (int j=1;j<i;j++) 13 { 14 if (i%j==0) dp[i]=Math.min(dp[i],dp[j] + i/j); 15 } 16 } 17 return dp[n]; 18 }
分类:
leetcode_dp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!