LeetCode
来源:互联网 发布:知乎 多功能料理机 编辑:程序博客网 时间:2024/06/05 22:46
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: 3Output: 3Explanation: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'.
Note:
- The
n
will be in the range [1, 1000].
初始有一个A,现在只有两种操作。操作1:赋值所有的A;操作2:进行粘贴。问最少操作几次得到确切的n个A
动态规划,dp[i] = min(dp[i], dp[j] + i / j),跟每个数的因子有关系。时间复杂度O(n^2),空间复杂度O(n)
class Solution {public: int minSteps(int n) { vector<int> dp(n+1); dp[0] = 0; dp[1] = 0; for (int i = 2; i <= n; ++i) { dp[i] = i; for (int j = i - 1; j >= 2; --j) { if (i % j) continue; dp[i] = min(dp[i], dp[j] + i / j); break; } } return dp[n]; }};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 剑指offer:多行打印二叉树
- Tornado源码之褪去模板(04)
- OpenCV学习之角点检测
- 小希的迷宫
- 使用Unity画一条平滑曲线(贝塞尔曲线)并使小球沿曲线运动
- LeetCode
- Locust性能测试框架介绍与安装
- Tip16 元素数量可变的情况下不应使用数组
- 执行shell命令工具类
- python3,使用sys.setdefaultencoding('utf-8'),编译时报错
- POJ 3259 用Bellman-Ford判断负环
- iOS app上架规则
- javascript学习(十三)— 标准对象
- 移动端固定输入框在底部会被键盘遮挡的解决方法