[LeetCode] Integer Break

343. Integer Break

Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.

Return the maximum product you can get.

1
2
3
4
5
6
7
8
9
10
class Solution {
private:
int integerBreakHelper(int n) {
return n > 4 ? 3 * integerBreakHelper(n - 3) : n;
}
public:
int integerBreak(int n) {
return n <= 3 ? n - 1 : integerBreakHelper(n);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/06/20/PS/LeetCode/integer-break/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.