comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
给定一个正整数 n
,将其拆分为 k
个 正整数 的和( k >= 2
),并使这些整数的乘积最大化。
返回 你可以获得的最大乘积 。
示例 1:
输入: n = 2 输出: 1 解释: 2 = 1 + 1, 1 × 1 = 1。
示例 2:
输入: n = 10 输出: 36 解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。
提示:
2 <= n <= 58
我们定义
考虑
- 将
$i$ 拆分成$i - j$ 和$j$ 的和,不继续拆分,此时乘积为$(i - j) \times j$ ; - 将
$i$ 拆分成$i - j$ 和$j$ 的和,继续拆分,此时乘积为$f[i - j] \times j$ 。
因此,我们可以得到状态转移方程:
最后返回
时间复杂度
class Solution:
def integerBreak(self, n: int) -> int:
f = [1] * (n + 1)
for i in range(2, n + 1):
for j in range(1, i):
f[i] = max(f[i], f[i - j] * j, (i - j) * j)
return f[n]
class Solution {
public int integerBreak(int n) {
int[] f = new int[n + 1];
f[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j < i; ++j) {
f[i] = Math.max(Math.max(f[i], f[i - j] * j), (i - j) * j);
}
}
return f[n];
}
}
class Solution {
public:
int integerBreak(int n) {
vector<int> f(n + 1);
f[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j < i; ++j) {
f[i] = max({f[i], f[i - j] * j, (i - j) * j});
}
}
return f[n];
}
};
func integerBreak(n int) int {
f := make([]int, n+1)
f[1] = 1
for i := 2; i <= n; i++ {
for j := 1; j < i; j++ {
f[i] = max(max(f[i], f[i-j]*j), (i-j)*j)
}
}
return f[n]
}
function integerBreak(n: number): number {
const f = Array(n + 1).fill(1);
for (let i = 3; i <= n; i++) {
for (let j = 1; j < i; j++) {
f[i] = Math.max(f[i], j * (i - j), j * f[i - j]);
}
}
return f[n];
}
impl Solution {
pub fn integer_break(n: i32) -> i32 {
let n = n as usize;
let mut f = vec![0; n + 1];
f[1] = 1;
for i in 2..=n {
for j in 1..i {
f[i] = f[i].max(f[i - j] * j).max((i - j) * j);
}
}
f[n] as i32
}
}
/**
* @param {number} n
* @return {number}
*/
var integerBreak = function (n) {
const f = Array(n + 1).fill(1);
for (let i = 2; i <= n; ++i) {
for (let j = 1; j < i; ++j) {
f[i] = Math.max(f[i], f[i - j] * j, (i - j) * j);
}
}
return f[n];
};
public class Solution {
public int IntegerBreak(int n) {
int[] f = new int[n + 1];
f[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j < i; ++j) {
f[i] = Math.Max(Math.Max(f[i], f[i - j] * j), (i - j) * j);
}
}
return f[n];
}
}
#define max(a, b) (((a) > (b)) ? (a) : (b))
int integerBreak(int n) {
int* f = (int*) malloc((n + 1) * sizeof(int));
f[1] = 1;
for (int i = 2; i <= n; ++i) {
f[i] = 0;
for (int j = 1; j < i; ++j) {
f[i] = max(f[i], max(f[i - j] * j, (i - j) * j));
}
}
return f[n];
}
当
时间复杂度
class Solution:
def integerBreak(self, n: int) -> int:
if n < 4:
return n - 1
if n % 3 == 0:
return pow(3, n // 3)
if n % 3 == 1:
return pow(3, n // 3 - 1) * 4
return pow(3, n // 3) * 2
class Solution {
public int integerBreak(int n) {
if (n < 4) {
return n - 1;
}
if (n % 3 == 0) {
return (int) Math.pow(3, n / 3);
}
if (n % 3 == 1) {
return (int) Math.pow(3, n / 3 - 1) * 4;
}
return (int) Math.pow(3, n / 3) * 2;
}
}
class Solution {
public:
int integerBreak(int n) {
if (n < 4) {
return n - 1;
}
if (n % 3 == 0) {
return pow(3, n / 3);
}
if (n % 3 == 1) {
return pow(3, n / 3 - 1) * 4;
}
return pow(3, n / 3) * 2;
}
};
func integerBreak(n int) int {
if n < 4 {
return n - 1
}
if n%3 == 0 {
return int(math.Pow(3, float64(n/3)))
}
if n%3 == 1 {
return int(math.Pow(3, float64(n/3-1))) * 4
}
return int(math.Pow(3, float64(n/3))) * 2
}
function integerBreak(n: number): number {
if (n < 4) {
return n - 1;
}
const m = Math.floor(n / 3);
if (n % 3 == 0) {
return 3 ** m;
}
if (n % 3 == 1) {
return 3 ** (m - 1) * 4;
}
return 3 ** m * 2;
}
impl Solution {
pub fn integer_break(n: i32) -> i32 {
if n < 4 {
return n - 1;
}
match n % 3 {
0 => return (3 as i32).pow((n / 3) as u32),
1 => return (3 as i32).pow((n / 3 - 1) as u32) * 4,
_ => return (3 as i32).pow((n / 3) as u32) * 2,
}
}
}
/**
* @param {number} n
* @return {number}
*/
var integerBreak = function (n) {
if (n < 4) {
return n - 1;
}
const m = Math.floor(n / 3);
if (n % 3 == 0) {
return 3 ** m;
}
if (n % 3 == 1) {
return 3 ** (m - 1) * 4;
}
return 3 ** m * 2;
};
public class Solution {
public int IntegerBreak(int n) {
if (n < 4) {
return n - 1;
}
if (n % 3 == 0) {
return (int)Math.Pow(3, n / 3);
}
if (n % 3 == 1) {
return (int)Math.Pow(3, n / 3 - 1) * 4;
}
return (int)Math.Pow(3, n / 3) * 2;
}
}
int integerBreak(int n) {
if (n < 4) {
return n - 1;
}
if (n % 3 == 0) {
return (int) pow(3, n / 3);
}
if (n % 3 == 1) {
return (int) pow(3, n / 3 - 1) * 4;
}
return (int) pow(3, n / 3) * 2;
}