class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
res = 1
inc, dec = 1, 0
preNum = 1
for i in range(1, n):
if ratings[i] >= ratings[i-1]:
preNum = 1 if ratings[i] == ratings[i-1] else preNum+1
res += preNum
inc = preNum
dec = 0
else:
dec += 1
if dec == inc:
dec += 1
res += dec
preNum = 1
return res
分发糖果-贪心135-python
最新推荐文章于 2024-08-28 10:11:17 发布