Skip to content

Commit bfef5c3

Browse files
committed
candy
1 parent a65d08b commit bfef5c3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
118118
#### [132. palindrome partitioning II](https://github.com/hitzzc/go-leetcode/tree/master/palindrome_partitioning_II)
119119
#### [133. Clone Graph](https://github.com/hitzzc/go-leetcode/tree/master/clone_graph)
120120
#### [134. gas station](https://github.com/hitzzc/go-leetcode/tree/master/gas_station)
121+
#### [135. candy](https://github.com/hitzzc/go-leetcode/tree/master/candy)
121122

122123

123124

candy/candy.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package candy
2+
3+
func candy(ratings []int) int {
4+
if len(ratings) == 0 {
5+
return 0
6+
}
7+
reduceLength, growLength := 0, 1
8+
9+
pre := 1
10+
ret := 1
11+
for i := 1; i < len(ratings); i++ {
12+
if ratings[i] < ratings[i-1] {
13+
reduceLength++
14+
if reduceLength >= pre {
15+
ret++
16+
}
17+
ret += reduceLength
18+
growLength = 1
19+
} else {
20+
cur := 1
21+
if ratings[i] > ratings[i-1] {
22+
cur = growLength + 1
23+
}
24+
growLength = cur
25+
ret += cur
26+
reduceLength = 0
27+
pre = cur
28+
}
29+
}
30+
return ret
31+
}

0 commit comments

Comments
 (0)