From 8a31e8cf79d5684ad56d014dd20fbbe747126346 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 20 Jul 2024 08:50:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0343 No.0343.Integer Break --- .../README.md" | 15 +- .../0300-0399/0343.Integer Break/README.md | 189 ++++++++++++++--- .../0300-0399/0343.Integer Break/README_EN.md | 197 +++++++++++++++--- .../0300-0399/0343.Integer Break/Solution.c | 14 +- .../0300-0399/0343.Integer Break/Solution.cpp | 8 +- .../0300-0399/0343.Integer Break/Solution.cs | 12 ++ .../0300-0399/0343.Integer Break/Solution.go | 8 +- .../0343.Integer Break/Solution.java | 8 +- .../0300-0399/0343.Integer Break/Solution.js | 13 ++ .../0300-0399/0343.Integer Break/Solution.py | 6 +- .../0300-0399/0343.Integer Break/Solution.rs | 12 +- .../0300-0399/0343.Integer Break/Solution.ts | 6 +- .../0300-0399/0343.Integer Break/Solution2.c | 12 ++ .../0300-0399/0343.Integer Break/Solution2.cs | 14 ++ .../0300-0399/0343.Integer Break/Solution2.js | 17 ++ .../0300-0399/0343.Integer Break/Solution2.rs | 12 ++ 16 files changed, 454 insertions(+), 89 deletions(-) create mode 100644 solution/0300-0399/0343.Integer Break/Solution.cs create mode 100644 solution/0300-0399/0343.Integer Break/Solution.js create mode 100644 solution/0300-0399/0343.Integer Break/Solution2.c create mode 100644 solution/0300-0399/0343.Integer Break/Solution2.cs create mode 100644 solution/0300-0399/0343.Integer Break/Solution2.js create mode 100644 solution/0300-0399/0343.Integer Break/Solution2.rs diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" index 7bd4e7328cbec..f80ac7c165001 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" @@ -42,15 +42,22 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9 ### 方法一:动态规划 -我们定义 $f[i]$ 表示正整数 $n$ 能获得的最大乘积,初始化 $f[1] = 1$。答案即为 $f[n]$。 +我们定义 $f[i]$ 表示正整数 $i$ 拆分后能获得的最大乘积,初始时 $f[1] = 1$。答案即为 $f[n]$。 -状态转移方程为: +考虑 $i$ 最后拆分出的数字 $j$,其中 $j \in [1, i)$。对于 $i$ 拆分出的数字 $j$,有两种情况: + +1. 将 $i$ 拆分成 $i - j$ 和 $j$ 的和,不继续拆分,此时乘积为 $(i - j) \times j$; +2. 将 $i$ 拆分成 $i - j$ 和 $j$ 的和,继续拆分,此时乘积为 $f[i - j] \times j$。 + +因此,我们可以得到状态转移方程: $$ f[i] = \max(f[i], f[i - j] \times j, (i - j) \times j) \quad (j \in [0, i)) $$ -时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为正整数 $n$。 +最后返回 $f[n]$ 即可。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为给定的正整数。 @@ -208,7 +215,7 @@ class Solution { ### 方法二:数学 -当 $n \lt 4$,此时 $n$ 不能拆分成至少两个正整数的和,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。 +当 $n \lt 4$ 时,由于题目要求至少剪一次,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。 时间复杂度 $O(1)$,空间复杂度 $O(1)$。 diff --git a/solution/0300-0399/0343.Integer Break/README.md b/solution/0300-0399/0343.Integer Break/README.md index e85a34a05c96c..c2585a7c6eb49 100644 --- a/solution/0300-0399/0343.Integer Break/README.md +++ b/solution/0300-0399/0343.Integer Break/README.md @@ -53,15 +53,22 @@ tags: ### 方法一:动态规划 -我们定义 $dp[i]$ 表示正整数 $n$ 能获得的最大乘积,初始化 $dp[1] = 1$。答案即为 $dp[n]$。 +我们定义 $f[i]$ 表示正整数 $i$ 拆分后能获得的最大乘积,初始时 $f[1] = 1$。答案即为 $f[n]$。 -状态转移方程为: +考虑 $i$ 最后拆分出的数字 $j$,其中 $j \in [1, i)$。对于 $i$ 拆分出的数字 $j$,有两种情况: + +1. 将 $i$ 拆分成 $i - j$ 和 $j$ 的和,不继续拆分,此时乘积为 $(i - j) \times j$; +2. 将 $i$ 拆分成 $i - j$ 和 $j$ 的和,继续拆分,此时乘积为 $f[i - j] \times j$。 + +因此,我们可以得到状态转移方程: $$ -dp[i] = max(dp[i], dp[i - j] \times j, (i - j) \times j) \quad (j \in [0, i)) +f[i] = \max(f[i], f[i - j] \times j, (i - j) \times j) \quad (j \in [0, i)) $$ -时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为正整数 $n$。 +最后返回 $f[n]$ 即可。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为给定的正整数。 @@ -70,11 +77,11 @@ $$ ```python class Solution: def integerBreak(self, n: int) -> int: - dp = [1] * (n + 1) + f = [1] * (n + 1) for i in range(2, n + 1): for j in range(1, i): - dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j) - return dp[n] + f[i] = max(f[i], f[i - j] * j, (i - j) * j) + return f[n] ``` #### Java @@ -82,14 +89,14 @@ class Solution: ```java class Solution { public int integerBreak(int n) { - int[] dp = new int[n + 1]; - dp[1] = 1; + int[] f = new int[n + 1]; + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = Math.max(Math.max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = Math.max(Math.max(f[i], f[i - j] * j), (i - j) * j); } } - return dp[n]; + return f[n]; } } ``` @@ -100,14 +107,14 @@ class Solution { class Solution { public: int integerBreak(int n) { - vector dp(n + 1); - dp[1] = 1; + vector f(n + 1); + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = max(max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = max({f[i], f[i - j] * j, (i - j) * j}); } } - return dp[n]; + return f[n]; } }; ``` @@ -116,14 +123,14 @@ public: ```go func integerBreak(n int) int { - dp := make([]int, n+1) - dp[1] = 1 + f := make([]int, n+1) + f[1] = 1 for i := 2; i <= n; i++ { for j := 1; j < i; j++ { - dp[i] = max(max(dp[i], dp[i-j]*j), (i-j)*j) + f[i] = max(max(f[i], f[i-j]*j), (i-j)*j) } } - return dp[n] + return f[n] } ``` @@ -131,13 +138,13 @@ func integerBreak(n int) int { ```ts function integerBreak(n: number): number { - let dp = new Array(n + 1).fill(1); + const f = Array(n + 1).fill(1); for (let i = 3; i <= n; i++) { for (let j = 1; j < i; j++) { - dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]); + f[i] = Math.max(f[i], j * (i - j), j * f[i - j]); } } - return dp.pop(); + return f[n]; } ``` @@ -146,11 +153,50 @@ function integerBreak(n: number): number { ```rust impl Solution { pub fn integer_break(n: i32) -> i32 { - if n < 4 { - return n - 1; + 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 + } +} +``` + +#### JavaScript + +```js +/** + * @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]; +}; +``` + +#### C# + +```cs +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); + } } - let count = (n - 2) / 3; - (3i32).pow(count as u32) * (n - count * 3) + return f[n]; } } ``` @@ -158,12 +204,18 @@ impl Solution { #### C ```c +#define max(a, b) (((a) > (b)) ? (a) : (b)) + int integerBreak(int n) { - if (n < 4) { - return n - 1; + 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)); + } } - int count = (n - 2) / 3; - return pow(3, count) * (n - count * 3); + return f[n]; } ``` @@ -175,7 +227,7 @@ int integerBreak(int n) { ### 方法二:数学 -当 $n \lt 4$ 时,$n$ 不能拆分成至少两个正整数的和,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。 +当 $n \lt 4$ 时,由于题目要求至少拆分成两个整数,因此 $n - 1$ 是最大乘积。当 $n \ge 4$ 时,我们尽可能多地拆分 $3$,当剩下的最后一段为 $4$ 时,我们将其拆分为 $2 + 2$,这样乘积最大。 时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -269,6 +321,81 @@ function integerBreak(n: number): number { } ``` +#### Rust + +```rust +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, + } + } +} +``` + +#### JavaScript + +```js +/** + * @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; +}; +``` + +#### C# + +```cs +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; + } +} +``` + +#### C + +```c +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; +} +``` + diff --git a/solution/0300-0399/0343.Integer Break/README_EN.md b/solution/0300-0399/0343.Integer Break/README_EN.md index c5da4dcec24a9..8b3ffb7eff47e 100644 --- a/solution/0300-0399/0343.Integer Break/README_EN.md +++ b/solution/0300-0399/0343.Integer Break/README_EN.md @@ -51,7 +51,24 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i]$ as the maximum product that can be obtained by splitting the positive integer $i$, with an initial condition of $f[1] = 1$. The answer is $f[n]$. + +Consider the last number $j$ split from $i$, where $j \in [1, i)$. For the number $j$ split from $i$, there are two cases: + +1. Split $i$ into the sum of $i - j$ and $j$, without further splitting, where the product is $(i - j) \times j$; +2. Split $i$ into the sum of $i - j$ and $j$, and continue splitting, where the product is $f[i - j] \times j$. + +Therefore, we can derive the state transition equation: + +$$ +f[i] = \max(f[i], f[i - j] \times j, (i - j) \times j) \quad (j \in [0, i)) +$$ + +Finally, returning $f[n]$ will suffice. + +The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the given positive integer. @@ -60,11 +77,11 @@ tags: ```python class Solution: def integerBreak(self, n: int) -> int: - dp = [1] * (n + 1) + f = [1] * (n + 1) for i in range(2, n + 1): for j in range(1, i): - dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j) - return dp[n] + f[i] = max(f[i], f[i - j] * j, (i - j) * j) + return f[n] ``` #### Java @@ -72,14 +89,14 @@ class Solution: ```java class Solution { public int integerBreak(int n) { - int[] dp = new int[n + 1]; - dp[1] = 1; + int[] f = new int[n + 1]; + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = Math.max(Math.max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = Math.max(Math.max(f[i], f[i - j] * j), (i - j) * j); } } - return dp[n]; + return f[n]; } } ``` @@ -90,14 +107,14 @@ class Solution { class Solution { public: int integerBreak(int n) { - vector dp(n + 1); - dp[1] = 1; + vector f(n + 1); + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = max(max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = max({f[i], f[i - j] * j, (i - j) * j}); } } - return dp[n]; + return f[n]; } }; ``` @@ -106,14 +123,14 @@ public: ```go func integerBreak(n int) int { - dp := make([]int, n+1) - dp[1] = 1 + f := make([]int, n+1) + f[1] = 1 for i := 2; i <= n; i++ { for j := 1; j < i; j++ { - dp[i] = max(max(dp[i], dp[i-j]*j), (i-j)*j) + f[i] = max(max(f[i], f[i-j]*j), (i-j)*j) } } - return dp[n] + return f[n] } ``` @@ -121,13 +138,13 @@ func integerBreak(n int) int { ```ts function integerBreak(n: number): number { - let dp = new Array(n + 1).fill(1); + const f = Array(n + 1).fill(1); for (let i = 3; i <= n; i++) { for (let j = 1; j < i; j++) { - dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]); + f[i] = Math.max(f[i], j * (i - j), j * f[i - j]); } } - return dp.pop(); + return f[n]; } ``` @@ -136,11 +153,50 @@ function integerBreak(n: number): number { ```rust impl Solution { pub fn integer_break(n: i32) -> i32 { - if n < 4 { - return n - 1; + 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 + } +} +``` + +#### JavaScript + +```js +/** + * @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]; +}; +``` + +#### C# + +```cs +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); + } } - let count = (n - 2) / 3; - (3i32).pow(count as u32) * (n - count * 3) + return f[n]; } } ``` @@ -148,12 +204,18 @@ impl Solution { #### C ```c +#define max(a, b) (((a) > (b)) ? (a) : (b)) + int integerBreak(int n) { - if (n < 4) { - return n - 1; + 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)); + } } - int count = (n - 2) / 3; - return pow(3, count) * (n - count * 3); + return f[n]; } ``` @@ -163,7 +225,11 @@ int integerBreak(int n) { -### Solution 2 +### Solution 1: Mathematics + +When $n < 4$, since the problem requires splitting into at least two integers, $n - 1$ yields the maximum product. When $n \geq 4$, we split into as many $3$s as possible. If the last segment remaining is $4$, we split it into $2 + 2$ for the maximum product. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. @@ -255,6 +321,81 @@ function integerBreak(n: number): number { } ``` +#### Rust + +```rust +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, + } + } +} +``` + +#### JavaScript + +```js +/** + * @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; +}; +``` + +#### C# + +```cs +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; + } +} +``` + +#### C + +```c +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; +} +``` + diff --git a/solution/0300-0399/0343.Integer Break/Solution.c b/solution/0300-0399/0343.Integer Break/Solution.c index 2cecd56a0f623..0dc021ee16f1c 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.c +++ b/solution/0300-0399/0343.Integer Break/Solution.c @@ -1,7 +1,13 @@ +#define max(a, b) (((a) > (b)) ? (a) : (b)) + int integerBreak(int n) { - if (n < 4) { - return n - 1; + 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)); + } } - int count = (n - 2) / 3; - return pow(3, count) * (n - count * 3); + return f[n]; } \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution.cpp b/solution/0300-0399/0343.Integer Break/Solution.cpp index 6ceba393ee4fa..14722f3698d08 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.cpp +++ b/solution/0300-0399/0343.Integer Break/Solution.cpp @@ -1,13 +1,13 @@ class Solution { public: int integerBreak(int n) { - vector dp(n + 1); - dp[1] = 1; + vector f(n + 1); + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = max(max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = max({f[i], f[i - j] * j, (i - j) * j}); } } - return dp[n]; + return f[n]; } }; \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution.cs b/solution/0300-0399/0343.Integer Break/Solution.cs new file mode 100644 index 0000000000000..e4cb8e81bb0b3 --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution.cs @@ -0,0 +1,12 @@ +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]; + } +} \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution.go b/solution/0300-0399/0343.Integer Break/Solution.go index c2de2799d04ed..cc28189665211 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.go +++ b/solution/0300-0399/0343.Integer Break/Solution.go @@ -1,10 +1,10 @@ func integerBreak(n int) int { - dp := make([]int, n+1) - dp[1] = 1 + f := make([]int, n+1) + f[1] = 1 for i := 2; i <= n; i++ { for j := 1; j < i; j++ { - dp[i] = max(max(dp[i], dp[i-j]*j), (i-j)*j) + f[i] = max(max(f[i], f[i-j]*j), (i-j)*j) } } - return dp[n] + return f[n] } \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution.java b/solution/0300-0399/0343.Integer Break/Solution.java index ebc48c40f0086..923f4f4811de9 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.java +++ b/solution/0300-0399/0343.Integer Break/Solution.java @@ -1,12 +1,12 @@ class Solution { public int integerBreak(int n) { - int[] dp = new int[n + 1]; - dp[1] = 1; + int[] f = new int[n + 1]; + f[1] = 1; for (int i = 2; i <= n; ++i) { for (int j = 1; j < i; ++j) { - dp[i] = Math.max(Math.max(dp[i], dp[i - j] * j), (i - j) * j); + f[i] = Math.max(Math.max(f[i], f[i - j] * j), (i - j) * j); } } - return dp[n]; + return f[n]; } } \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution.js b/solution/0300-0399/0343.Integer Break/Solution.js new file mode 100644 index 0000000000000..34b210f401724 --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution.js @@ -0,0 +1,13 @@ +/** + * @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]; +}; diff --git a/solution/0300-0399/0343.Integer Break/Solution.py b/solution/0300-0399/0343.Integer Break/Solution.py index faebd81349904..5c7fb26cd7fe7 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.py +++ b/solution/0300-0399/0343.Integer Break/Solution.py @@ -1,7 +1,7 @@ class Solution: def integerBreak(self, n: int) -> int: - dp = [1] * (n + 1) + f = [1] * (n + 1) for i in range(2, n + 1): for j in range(1, i): - dp[i] = max(dp[i], dp[i - j] * j, (i - j) * j) - return dp[n] + f[i] = max(f[i], f[i - j] * j, (i - j) * j) + return f[n] diff --git a/solution/0300-0399/0343.Integer Break/Solution.rs b/solution/0300-0399/0343.Integer Break/Solution.rs index cbd12dbf0bbf0..2256aeee28fa9 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.rs +++ b/solution/0300-0399/0343.Integer Break/Solution.rs @@ -1,9 +1,13 @@ impl Solution { pub fn integer_break(n: i32) -> i32 { - if n < 4 { - return n - 1; + 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); + } } - let count = (n - 2) / 3; - (3i32).pow(count as u32) * (n - count * 3) + f[n] as i32 } } diff --git a/solution/0300-0399/0343.Integer Break/Solution.ts b/solution/0300-0399/0343.Integer Break/Solution.ts index bedef9889f279..5239b8eb68fde 100644 --- a/solution/0300-0399/0343.Integer Break/Solution.ts +++ b/solution/0300-0399/0343.Integer Break/Solution.ts @@ -1,9 +1,9 @@ function integerBreak(n: number): number { - let dp = new Array(n + 1).fill(1); + const f = Array(n + 1).fill(1); for (let i = 3; i <= n; i++) { for (let j = 1; j < i; j++) { - dp[i] = Math.max(dp[i], j * (i - j), j * dp[i - j]); + f[i] = Math.max(f[i], j * (i - j), j * f[i - j]); } } - return dp.pop(); + return f[n]; } diff --git a/solution/0300-0399/0343.Integer Break/Solution2.c b/solution/0300-0399/0343.Integer Break/Solution2.c new file mode 100644 index 0000000000000..46a8519dcd793 --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution2.c @@ -0,0 +1,12 @@ +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; +} \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution2.cs b/solution/0300-0399/0343.Integer Break/Solution2.cs new file mode 100644 index 0000000000000..77a5a6375613e --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution2.cs @@ -0,0 +1,14 @@ +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; + } +} \ No newline at end of file diff --git a/solution/0300-0399/0343.Integer Break/Solution2.js b/solution/0300-0399/0343.Integer Break/Solution2.js new file mode 100644 index 0000000000000..c1d5a9b5debcf --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution2.js @@ -0,0 +1,17 @@ +/** + * @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; +}; diff --git a/solution/0300-0399/0343.Integer Break/Solution2.rs b/solution/0300-0399/0343.Integer Break/Solution2.rs new file mode 100644 index 0000000000000..031f1f1ad5698 --- /dev/null +++ b/solution/0300-0399/0343.Integer Break/Solution2.rs @@ -0,0 +1,12 @@ +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, + } + } +}