-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathIntegerBreak343.java
More file actions
51 lines (46 loc) · 1.22 KB
/
IntegerBreak343.java
File metadata and controls
51 lines (46 loc) · 1.22 KB
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
/**
* Given a positive integer n, break it into the sum of at least two positive
* integers and maximize the product of those integers. Return the maximum
* product you can get.
*
* Example 1:
* Input: 2
* Output: 1
* Explanation: 2 = 1 + 1, 1 × 1 = 1.
*
* Example 2:
* Input: 10
* Output: 36
* Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
* Note: You may assume that n is not less than 2 and not larger than 58.
*/
public class IntegerBreak343 {
public int integerBreak(int n) {
int[] dp = new int[n+1];
dp[1] = 1;
for (int i=2; i<=n; i++) {
int tmp = 0;
for (int j=1; j<i; j++) {
int k = i - j;
tmp = Math.max(tmp, j * k);
tmp = Math.max(tmp, j * dp[k]);
}
dp[i] = tmp;
}
return dp[n];
}
/**
* https://leetcode.com/problems/integer-break/discuss/80689/A-simple-explanation-of-the-math-part-and-a-O(n)-solution
*/
public int integerBreak2(int n) {
if(n==2) return 1;
if(n==3) return 2;
int product = 1;
while(n>4){
product*=3;
n-=3;
}
product*=n;
return product;
}
}