贪心,根据当前孩子的左右两侧分数比较得到孩子应该得到的糖果,由于要符合两侧的要求取较大值
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# pick candy
# @param arr int整型一维数组 the array
# @return int整型
#
class Solution:
def candy(self , arr: List[int]) -> int:
# write code here
if len(arr) == 1:
return 1
data = [0 for _ in range(len(arr))]
l, r = [0 for _ in range(len(arr))], [0 for _ in range(len(arr))]
l[0], r[-1] = 1, 1
for i in range(1, len(arr)):
if arr[i] > arr[i - 1]:
l[i] = l[i - 1] + 1
else:
l[i] = 1
for i in range(len(arr) - 2, -1, -1):
if arr[i] > arr[i + 1]:
r[i] = r[i + 1] + 1
else:
r[i] = 1
for i in range(len(arr)):
data[i] = max(l[i], r[i])
return sum(data)