Skip to content

Commit 3bbc0bd

Browse files
author
hero
committed
分发水果
1 parent 26c341a commit 3bbc0bd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

leet_code/candy_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leet_code
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func candy(ratings []int) (d int) {
8+
l := len(ratings)
9+
start := make([]int, l)
10+
for i := 0; i < l; i++ {
11+
if i > 0 && ratings[i] > ratings[i-1] {
12+
start[i] = start[i-1] + 1
13+
} else {
14+
start[i] = 1
15+
}
16+
}
17+
var end int
18+
for i := l - 1; i >= 0; i-- {
19+
if i < l-1 && ratings[i] > ratings[i+1] {
20+
end++
21+
} else {
22+
end = 1
23+
}
24+
d += max(end, start[i])
25+
}
26+
return d
27+
}
28+
29+
func Test_candy(t *testing.T) {
30+
t.Log(candy([]int{1, 0, 2}))
31+
}

0 commit comments

Comments
 (0)