From 49e386d20ba95edeffba686584a6b123597ddbba Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 18 Jan 2024 10:28:55 +0800 Subject: [PATCH] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F2?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1049.最后一块石头的重量II.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index cc661317..4c3c01a0 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -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]; + } +} +```