Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6233483

Browse files
Merge pull request doocs#223 from huaxu1024/master
Add Soulution2.java for 0343.Integer Break
2 parents edd3ed2 + f92327b commit 6233483

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Integer Break
2+
3+
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.
4+
5+
## Example 1:
6+
```
7+
Input: 2
8+
Output: 1
9+
Explanation: 2 = 1 + 1, 1 ×ばつ 1 = 1.
10+
```
11+
12+
## Example 2:
13+
```
14+
Input: 10
15+
Output: 36
16+
Explanation: 10 = 3 + 3 + 4, 3 ×ばつ 3 ×ばつ 4 = 36.
17+
```
18+
Note: You may assume that n is not less than 2 and not larger than 58.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* O(1) ==》 time complexity
3+
* O(1) ==》space complexity
4+
*/
5+
class Solution {
6+
public int integerBreak(int n) {
7+
if (n <= 3) return n-1;
8+
9+
int a = n/3, b = n%3;
10+
if (b == 0) return pow(3, a);
11+
if (b == 1) return pow(3, a-1) << 2;
12+
return pow(3, a) << 1;
13+
}
14+
15+
private int pow(int a, int b) {
16+
if (b == 0) return 1;
17+
return a * pow(a, b-1);
18+
}
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /