题解 | #分糖果问题#
分糖果问题
https://www.nowcoder.com/practice/76039109dd0b47e994c08d8319faa352
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ public int candy (List<int> arr) { int cur = 0; int curc = 1; int pre = 1; int res = 1; for(int i = 1; i < arr.Count; i++){ if(arr[i] > arr[i-1]){ res += pre + 1; pre++; cur = i; curc = pre; } else if(arr[i] == arr[i - 1]){ cur = i; res++; pre = 1; curc = pre; } else{ res += i - cur; if(i - cur >= curc) res++; pre = 1; } //Console.WriteLine(res); } return res; } }