题目链接:https://leetcode.com/problems/2-keys-keyboard/description/
解题思路:要求原问题的解,先求出其子问题的解。要计算合成n的最少步骤,就要计算上次复制之前的最少步骤,如此类推。
代码如下:
class Solution {
public:
int minSteps(int n) {
if (n == 1) return 0;
vector<int> a;
for (int i = 1; i < n; i++) {
if (n%i == 0) {
a.push_back(i);
}
}
int min = n;
for (int i = 0; i < a.size(); i++) {
int min_i = minSteps(a[i]) + n/a[i];
min = (min < min_i) ? min : min_i;
}
return min;
}
};