Update0135.分发糖果,添加C#

This commit is contained in:
涛 陈 2024-01-02 09:50:19 +08:00
parent c9bff6e42c
commit 4f2b2b2618
1 changed files with 29 additions and 0 deletions

View File

@ -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();
}
}
```
<p align="center"> <p align="center">