题解 | #分糖果问题#
分糖果问题
https://www.nowcoder.com/practice/76039109dd0b47e994c08d8319faa352
class Solution: def candy(self , arr: List[int]) -> int: # write code here l = len(arr) if l < 2: return l res = [1] * l # left ---> right for i in range(1, l): if arr[i] > arr[i-1]: res[i] = res[i-1] + 1 # left <--- right for i in range(l-2, -1, -1): if arr[i] > arr[i+1]: res[i] = max(res[i], res[i+1] + 1) return sum(res)