1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| func candy(ratings []int) int { candy := make([]int, len(ratings)) sum := 0 for i := 0; i < len(ratings); i++ { candy[i] = 1 }
for i := 0; i < len(ratings)-1; i++ { if ratings[i] < ratings[i+1] { candy[i+1] = candy[i] + 1 } }
for i := len(ratings) - 1; i > 0; i-- { if ratings[i-1] > ratings[i] { candy[i-1] = findMax(candy[i-1], candy[i]+1) } } for i := 0; i < len(ratings); i++ { sum += candy[i] } return sum }
func findMax(num1 int, num2 int) int { if num1 > num2 { return num1 } return num2 }
|