leetcode dp题目:
来源:互联网 发布:mdf文件用mysql打开 编辑:程序博客网 时间:2024/05/17 18:25
- 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’.
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’.
难度中等吧:
class Solution {public: int minSteps(int n) { //动态规划 //要点 1.dp[0] = dp[1] = 0; //状态转移方程 ; dp[x] = min(dp[x],dp[y]+y/x) vector<int> dp(n+1,INT_MAX); dp[0] = 0,dp[1] = 0; for(int i=2; i <=n ; i++) { for(int j=1; j < i; j++) { if(i % j == 0) { dp[i] = min(dp[i],dp[j]+i/j); } } } return dp[n]; }};
阅读全文
0 0
- leetcode dp题目:
- leetcode -- Triangle -- dp题目重点
- leetcode -- Decode Ways -- dp题目
- leetcode中的DP题目总结
- Leetcode 动态规划DP类题目
- leetcode -- Range Sum Query 2D - Immutable -- 简单DP题目
- DP 题目
- dp题目
- DP题目
- dp 题目
- dp题目
- DP题目
- leetcode题目
- LeetCode 题目
- < 题目 > LeetCode
- 【DP】树形DP题目总结
- 9.10 叠箱子,不是很理解,建议做几道dp的leetcode题目
- leetcode -- Unique Paths I &&II-- 典型DP 题目,简单要看
- OTA后系统reboot失败的问题分析和解决
- HYSBZ
- 如何使用RedisTemplate访问Redis数据结构
- HDOJ2017_字符串统计
- 数独
- leetcode dp题目:
- 范数(Norm)和谱半径(Spectral Radii)
- Remove Duplicates from Sorted Array
- cocos2dx 开启控制台
- 【MongoDB】复制集 相关 (bully算法)
- html清除浮动的几种方法
- JAVA工程师学习路线
- [siggraph17]bungie的特效系统
- Unity3D三维坐标(X,Z轴)下物体拖动移动(贴地面移动)实现并实现物体碰撞检测