diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index c2fbbdde..4df6c41f 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -319,6 +319,29 @@ pub fn integer_break(n: i32) -> i32 { } ``` +贪心: + +```rust +impl Solution { + pub fn integer_break(mut n: i32) -> i32 { + match n { + 2 => 1, + 3 => 2, + 4 => 4, + 5.. => { + let mut res = 1; + while n > 4 { + res *= 3; + n -= 3; + } + res * n + } + _ => panic!("Error"), + } + } +} +``` + ### TypeScript ```typescript @@ -344,27 +367,6 @@ function integerBreak(n: number): number { }; ``` -### Rust - -```Rust -impl Solution { - fn max(a: i32, b: i32) -> i32{ - if a > b { a } else { b } - } - pub fn integer_break(n: i32) -> i32 { - let n = n as usize; - let mut dp = vec![0; n + 1]; - dp[2] = 1; - for i in 3..=n { - for j in 1..i - 1 { - dp[i] = Self::max(dp[i], Self::max(((i - j) * j) as i32, dp[i - j] * j as i32)); - } - } - dp[n] - } -} -``` - ### C ```c