-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
51 lines (49 loc) · 1.18 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
int steps[1010][1010];
public:
int solve(int cur_len, int copy_len, int N) {
if (cur_len == N) {
return 0;
}
if (cur_len > N) {
return -1;
}
if (steps[cur_len][copy_len] != -1) {
return steps[cur_len][copy_len];
}
int res = INT_MAX;
// copy if copy_len != cur_len
if (copy_len != cur_len) {
int next = solve(cur_len, cur_len, N);
if (next != -1) {
res = 1 + next;
}
}
// paste if copy_len > 0
if (copy_len > 0) {
int next = solve(cur_len + copy_len, copy_len, N);
if (next != -1) {
res = min(res, 1 + next);
}
}
if (res == INT_MAX) {
res = -1;
}
return steps[cur_len][copy_len] = res;
}
int minSteps(int n) {
if (n == 1) {
return 0;
}
memset(steps, -1, sizeof(steps));
return solve(1, 0, n);
}
};
int main () {
Solution solver;
cout << solver.minSteps(10) << endl;
return 0;
}