impl: q135
This commit is contained in:
parent
20cc46bcd0
commit
e09fffb809
1 changed files with 26 additions and 0 deletions
26
solutions/q135/solution.go
Normal file
26
solutions/q135/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q135
|
||||
|
||||
func candy(ratings []int) int {
|
||||
n := len(ratings)
|
||||
candies := make([]int, n)
|
||||
|
||||
for i := range n {
|
||||
candies[i] = 1
|
||||
if i > 0 && ratings[i-1] < ratings[i] {
|
||||
candies[i] = candies[i-1] + 1
|
||||
}
|
||||
}
|
||||
for i := n - 2; i >= 0; i-- {
|
||||
if ratings[i+1] < ratings[i] {
|
||||
candies[i] = max(candies[i], candies[i+1]+1)
|
||||
}
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, c := range candies {
|
||||
sum += c
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
var _ = candy
|
||||
Loading…
Add table
Add a link
Reference in a new issue