-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14_cut_the_rope.cc
55 lines (45 loc) · 1.21 KB
/
14_cut_the_rope.cc
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
52
53
54
55
#include <iostream>
using namespace std;
// 至少切一刀。
class Solution {
public:
int foo(int length) {
if (length <= 1) {
return 0;
}
if (length == 2) {
// 切成1+1两段。
return 1;
}
if (length == 3) {
// 切成1+2两段。
return 2;
}
// 之所以只要判断到length=3是因为在length=4时,可以被length=2+2的情况考虑到。
int *max = new int[length+1]();
max[0] = 0;
max[1] = 1;
max[2] = 2;
max[3] = 3;
for (int i = 4; i <= length; i++) {
for (int j = 1; j <= (i>>1); j++) { // j从1开始表示至少切一刀。
int tmp = max[j]*max[i-j];
if (tmp > max[i]) {
max[i] = tmp;
}
}
}
delete [] max;
return max[length];
}
};
int main(int argc, char const *argv[])
{
cout << Solution().foo(0) << endl;
cout << Solution().foo(1) << endl;
cout << Solution().foo(2) << endl;
cout << Solution().foo(3) << endl;
cout << Solution().foo(4) << endl;
cout << Solution().foo(8) << endl;
return 0;
}