Skip to content
Merged
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.DS_Store
44 changes: 23 additions & 21 deletions problems/0343.ๆ•ดๆ•ฐๆ‹†ๅˆ†.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,39 @@ object Solution {
}
```

### Rust

```rust
pub struct Solution;

impl Solution {
pub fn wei_bag_problem1(weight: Vec<usize>, value: Vec<usize>, bag_size: usize) -> usize {
let mut dp = vec![vec![0; bag_size + 1]; weight.len()];
for j in weight[0]..=weight.len() {
dp[0][j] = value[0];
}

for i in 1..weight.len() {
for j in 0..=bag_size {
match j < weight[i] {
true => dp[i][j] = dp[i - 1][j],
false => dp[i][j] = dp[i - 1][j].max(dp[i - 1][j - weight[i]] + value[i]),
}
}
}
dp[weight.len() - 1][bag_size]
}
}

#[test]
fn test_wei_bag_problem1() {
println!(
"{}",
Solution::wei_bag_problem1(vec![1, 3, 4], vec![15, 20, 30], 4)
);
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/็ฝ‘็ซ™ๆ˜Ÿ็ƒๅฎฃไผ ๆตทๆŠฅ.jpg" width="1000"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,34 @@ object Solution {
}
```

### Rust

```rust
pub struct Solution;

impl Solution {
pub fn wei_bag_problem2(weight: Vec<usize>, value: Vec<usize>, bag_size: usize) -> usize {
let mut dp = vec![0; bag_size + 1];
for i in 0..weight.len() {
for j in (weight[i]..=bag_size).rev() {
if j >= weight[i] {
dp[j] = dp[j].max(dp[j - weight[i]] + value[i]);
}
}
}
dp[dp.len() - 1]
}
}

#[test]
fn test_wei_bag_problem2() {
println!(
"{}",
Solution::wei_bag_problem2(vec![1, 3, 4], vec![15, 20, 30], 4)
);
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/็ฝ‘็ซ™ๆ˜Ÿ็ƒๅฎฃไผ ๆตทๆŠฅ.jpg" width="1000"/>
Expand Down