10000 refactor 343 · arnav1996/Leetcode-1@838be6b · GitHub
[go: up one dir, main page]

Skip to content

Commit 838be6b

Browse files
refactor 343
1 parent 5dfbb1d commit 838be6b

File tree

1 file changed

+15
-11
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-11
lines changed
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 343. Integer Break
5+
*
46
* 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.
57
68
For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
@@ -13,17 +15,19 @@ There is a simple O(n) solution to this problem.
1315
You may check the breaking results of n ranging from 7 to 10 to discover the regularities.
1416
*/
1517
public class _343 {
16-
public int integerBreak(int n) {
17-
if (n == 2) {
18-
return 1;
19-
} else if (n == 3) {
20-
return 2;
21-
} else if (n % 3 == 0) {
22-
return (int) Math.pow(3, n / 3);
23-
} else if (n % 3 == 1) {
24-
return 2 * 2 * (int) Math.pow(3, (n - 4) / 3);
25-
} else {
26-
return 2 * (int) Math.pow(3, n / 3);
18+
public st 87E6 atic class Solution1 {
19+
public int integerBreak(int n) {
20+
if (n == 2) {
21+
return 1;
22+
} else if (n == 3) {
23+
return 2;
24+
} else if (n % 3 == 0) {
25+
return (int) Math.pow(3, n / 3);
26+
} else if (n % 3 == 1) {
27+
return 2 * 2 * (int) Math.pow(3, (n - 4) / 3);
28+
} else {
29+
return 2 * (int) Math.pow(3, n / 3);
30+
}
2731
}
2832
}
2933
}

0 commit comments

Comments
 (0)
0