mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
21 lines
429 B
Go
21 lines
429 B
Go
package leetcode
|
|
|
|
func candy(ratings []int) int {
|
|
candies := make([]int, len(ratings))
|
|
for i := 1; i < len(ratings); i++ {
|
|
if ratings[i] > ratings[i-1] {
|
|
candies[i] += candies[i-1] + 1
|
|
}
|
|
}
|
|
for i := len(ratings) - 2; i >= 0; i-- {
|
|
if ratings[i] > ratings[i+1] && candies[i] <= candies[i+1] {
|
|
candies[i] = candies[i+1] + 1
|
|
}
|
|
}
|
|
total := 0
|
|
for _, candy := range candies {
|
|
total += candy + 1
|
|
}
|
|
return total
|
|
}
|