Update 0343.整数拆分.md

This commit is contained in:
fw_qaq
2023-04-13 18:51:26 +08:00
committed by GitHub
parent 3b8eff71cd
commit bd22ad9257

View File

@ -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