mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2085 from fwqaaq/patch-42
Update 1049.最后一块石头的重量II.md about rust
This commit is contained in:
@ -379,8 +379,23 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn last_stone_weight_ii(stones: Vec<i32>) -> i32 {
|
||||||
|
let sum = stones.iter().sum::<i32>();
|
||||||
|
let target = sum as usize / 2;
|
||||||
|
let mut dp = vec![0; target + 1];
|
||||||
|
for s in stones {
|
||||||
|
for j in (s as usize..=target).rev() {
|
||||||
|
dp[j] = dp[j].max(dp[j - s as usize] + s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum - dp[target] * 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user