[LeetCode] 650. 2 Keys Keyboard ## 题目描述
来源:互联网 发布:武义网络电视 编辑:程序博客网 时间:2024/05/24 01:37
[LeetCode] 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’.
Note:
The n will be in the range [1, 1000].
分析
当一个操作对更多的字母进行操作时,总的操作数更少,同时也要用更少的操作数获得更多的字母。对n进行因数分解,因数的总和为结果。
class Solution {public: int minSteps(int n) { int step = 0; while (n != 1) { for (int i = 2; i <= n; i++) { if (n % i == 0) { n /= i; step += i; break; } } } return step; }};
- [LeetCode] 650. 2 Keys Keyboard ## 题目描述
- leetcode 650. 2 Keys Keyboard
- LeetCode -- 650. 2 Keys Keyboard
- Leetcode 650. 2 Keys Keyboard
- [LeetCode] 650. 2 Keys Keyboard
- leetcode 650. 2 Keys Keyboard
- [LeetCode] 650. 2 Keys Keyboard
- Leetcode: 650. 2 Keys Keyboard
- [LeetCode] 650. 2 Keys Keyboard
- LeetCode-2 Keys Keyboard
- LeetCode:2 Keys Keyboard
- leetcode 650. 2 Keys Keyboard(dp)
- LeetCode 650. 2 Keys Keyboard--动态规划
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 650. 2 Keys Keyboard
- 正则%00截断
- android 获取ListView内部Item高度
- [LeetCode] 646. Maximum Length of Pair Chain
- java获取excel的.xlsx格式的具体某一列
- [LeetCode] 688. Knight Probability in Chessboard
- [LeetCode] 650. 2 Keys Keyboard ## 题目描述
- easyui 01火推
- Unique Paths II
- 密室逃脱中的悲剧
- 网易Java程序员两轮面试,这些问题你能答对几个?
- 女生征婚:喜欢懂云计算的
- QQ帐户的申请与登陆
- ScratchView:一步步打造万能的 Android 刮奖效果控件
- NYOJ 32 组合数(DFS)