From 4f2b2b2618d522d73ef15ac200799453691170c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=9B=20=E9=99=88?= <761050293@qq.com> Date: Tue, 2 Jan 2024 09:50:19 +0800 Subject: [PATCH] =?UTF-8?q?Update0135.=E5=88=86=E5=8F=91=E7=B3=96=E6=9E=9C?= =?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 --- problems/0135.分发糖果.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index d130bd68..210f4995 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -370,6 +370,35 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int Candy(int[] ratings) + { + int[] candies = new int[ratings.Length]; + for (int i = 0; i < candies.Length; i++) + { + candies[i] = 1; + } + for (int i = 1; i < ratings.Length; i++) + { + if (ratings[i] > ratings[i - 1]) + { + candies[i] = candies[i - 1] + 1; + } + } + for (int i = ratings.Length - 2; i >= 0; i--) + { + if (ratings[i] > ratings[i + 1]) + { + candies[i] = Math.Max(candies[i], candies[i + 1] + 1); + } + } + return candies.Sum(); + } +} +```