题目:
题解:
func candy(ratings []int) int {
n := len(ratings)
ans, inc, dec, pre := 1, 1, 0, 1
for i := 1; i < n; i++ {
if ratings[i] >= ratings[i-1] {
dec = 0
if ratings[i] == ratings[i-1] {
pre = 1
} else {
pre++
}
ans += pre
inc = pre
} else {
dec++
if dec == inc {
dec++
}
ans += dec
pre = 1
}
}
return ans
}