-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegerBreak.cpp
More file actions
35 lines (34 loc) · 796 Bytes
/
integerBreak.cpp
File metadata and controls
35 lines (34 loc) · 796 Bytes
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
// math
class Solution {
public:
int integerBreak(int n) {
if (n <= 3) {
return n - 1;
}
int quotient = n / 3;
int remainder = n % 3;
if (remainder == 0) {
return (int)pow(3, quotient);
} else if (remainder == 1) {
return (int)pow(3, quotient - 1) * 4;
} else {
return (int)pow(3, quotient) * 2;
}
}
};
// dp
class Solution {
public:
int integerBreak(int n) {
if (n < 4) return n - 1;
vector<int> dp(n + 1, 0);
dp[2] = 1;
dp[3] = 2;
for (int i = 4; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = std::max({dp[i - j] * j, (i - j) * j, dp[i]});
}
}
return dp[n];
}
};