Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1547fdc

Browse files
committedFeb 22, 2025
Add solution #343
1 parent 2af103e commit 1547fdc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
338|[Counting Bits](./0338-counting-bits.js)|Easy|
277277
341|[Flatten Nested List Iterator](./0341-flatten-nested-list-iterator.js)|Medium|
278278
342|[Power of Four](./0342-power-of-four.js)|Easy|
279+
343|[Integer Break](./0343-integer-break.js)|Medium|
279280
344|[Reverse String](./0344-reverse-string.js)|Easy|
280281
345|[Reverse Vowels of a String](./0345-reverse-vowels-of-a-string.js)|Easy|
281282
347|[Top K Frequent Elements](./0347-top-k-frequent-elements.js)|Medium|

‎solutions/0343-integer-break.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 343. Integer Break
3+
* https://leetcode.com/problems/integer-break/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer n, break it into the sum of k positive integers, where k >= 2,
7+
* and maximize the product of those integers.
8+
*
9+
* Return the maximum product you can get.
10+
*/
11+
12+
/**
13+
* @param {number} n
14+
* @return {number}
15+
*/
16+
var integerBreak = function(n) {
17+
if (n <= 3) return n - 1;
18+
let product = 1;
19+
while (n > 4) {
20+
product *= 3;
21+
n -= 3;
22+
}
23+
return product * n;
24+
};

0 commit comments

Comments
 (0)
Please sign in to comment.