Skip to content

Commit 5f62ce0

Browse files
committed
feat: solve No.343
1 parent 24362df commit 5f62ce0

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

301-400/343. Integer Break.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 343. Integer Break
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Math, Dynamic Programming.
5+
- Similar Questions: Maximize Number of Nice Divisors.
6+
7+
## Problem
8+
9+
Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.
10+
11+
Return **the maximum product you can get**.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: n = 2
18+
Output: 1
19+
Explanation: 2 = 1 + 1, 1 × 1 = 1.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: n = 10
26+
Output: 36
27+
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
28+
```
29+
30+
 
31+
**Constraints:**
32+
33+
34+
35+
- `2 <= n <= 58`
36+
37+
38+
39+
## Solution
40+
41+
```javascript
42+
/**
43+
* @param {number} n
44+
* @return {number}
45+
*/
46+
var integerBreak = function(n) {
47+
if (n < 4) return n - 1;
48+
var res = 1;
49+
while (n) {
50+
if (n > 4) {
51+
res *= 3;
52+
n -= 3;
53+
} else if (n <= 4 && n >= 2) {
54+
res *= n;
55+
n = 0;
56+
} else if (n === 1) {
57+
n = 0;
58+
}
59+
}
60+
return res;
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
nope.
67+
68+
**Complexity:**
69+
70+
* Time complexity : O(n).
71+
* Space complexity : O(1).

0 commit comments

Comments
 (0)