Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions solution/0343.Integer Break/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Integer Break

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.
19 changes: 19 additions & 0 deletions solution/0343.Integer Break/Solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* O(1) ==》 time complexity
* O(1) ==》space complexity
*/
class Solution {
public int integerBreak(int n) {
if (n <= 3) return n-1;

int a = n/3, b = n%3;
if (b == 0) return pow(3, a);
if (b == 1) return pow(3, a-1) << 2;
return pow(3, a) << 1;
}

private int pow(int a, int b) {
if (b == 0) return 1;
return a * pow(a, b-1);
}
}