-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIntegerBreak.java
68 lines (66 loc) · 2.06 KB
/
IntegerBreak.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
/*https://leetcode.com/problems/integer-break/*/
class Solution {
public int integerBreak(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
int[] table = new int[n+1];
table[0]=table[1] = 1;
table[2] = 1;
table[3] = 2;
int i, j, k;
for (i = 4; i <= n; ++i)
{
for (j = i/2; j >= 1; --j)
{
k = i-j;
if (j*k > table[i]) table[i] = j*k; //i = j+k
if (j*table[k] > table[i]) table[i] = j*table[k]; // i = j+k1+k2+k3+...+km where k = k1+k2+k3+...+km
if (table[j]*k > table[i]) table[i] = table[j]*k; // i = j1+j2+j3+...+jm+k where j = j1+j2+j3+...+jm
if (table[j]*table[k] > table[i]) table[i] = table[j]*table[k]; // i = j1+j2+j3+...+jm+k1+k2+k3+...+kn where k = k1+k2+k3+...+kn and j = j1+j2+j3+...+jn
}
}
return table[n];
}
}
class Solution {
public int integerBreak(int n) {
if (n == 2) return 1;
if (n == 3) return 2;
int[] table = new int[n+1];
table[0]=table[1] = 1;
table[2] = 1;
table[3] = 2;
int i, j, k;
boolean isUpdated = false;
for (i = 4; i <= n; ++i)
{
for (j = i/2; j >= 1; --j)
{
k = i-j;
isUpdated = false;
if (j*k > table[i])
{
isUpdated = true;
table[i] = j*k;
}
if (j*table[k] > table[i])
{
isUpdated = true;
table[i] = j*table[k];
}
if (table[j]*k > table[i])
{
isUpdated = true;
table[i] = table[j]*k;
}
if (table[j]*table[k] > table[i])
{
isUpdated = true;
table[i] = table[j]*table[k];
}
if (!isUpdated) break;
}
}
return table[n];
}
}