mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 1049.最后一块石头的重量2,添加C#
This commit is contained in:
@ -472,6 +472,30 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public int LastStoneWeightII(int[] stones)
|
||||
{
|
||||
int[] dp = new int[15001];
|
||||
int sum = 0;
|
||||
foreach (int stone in stones)
|
||||
{
|
||||
sum += stone;
|
||||
}
|
||||
int target = sum / 2;
|
||||
for (int i = 0; i < stones.Length; i++)
|
||||
{
|
||||
for (int j = target; j >= stones[i]; j--)
|
||||
{
|
||||
dp[j] = Math.Max(dp[j], dp[j - stones[i]] + stones[i]);
|
||||
}
|
||||
}
|
||||
return sum - 2 * dp[target];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user