-
Notifications
You must be signed in to change notification settings - Fork 0
/
135.go
86 lines (76 loc) · 1.6 KB
/
135.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package p135
/**
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
*/
func candy(ratings []int) int {
if len(ratings) <= 1 {
return len(ratings)
}
lastCandy, lastRating := 1, ratings[0]
candiesCount := 0
for i := 0; i < len(ratings); i++ {
v := ratings[i]
if v > lastRating {
lastRating = v
lastCandy++
candiesCount += lastCandy
} else if v == lastRating {
lastCandy = 1
candiesCount += lastCandy
} else {
highestValue := lastCandy
j := i
for j = i + 1; j < len(ratings); j++ {
if ratings[j] >= ratings[j-1] {
break
}
}
lastCandy = 1
candiesCount += ((j - i) + 1) * (j - i) / 2
if (j - i + 1) > highestValue {
candiesCount += (j - i + 1) - highestValue
}
lastCandy = 1
i = j - 1
lastRating = ratings[i]
continue
}
}
return candiesCount
}
func candy_another(ratings []int) int {
size := len(ratings)
if size <= 1 {
return size
}
max := func(a, b int) int {
if a > b {
return a
} else {
return b
}
}
num := make([]int, size)
for i := 0; i < size; i++ {
num[i] = 1
}
for i := 1; i < size; i++ {
if ratings[i] > ratings[i-1] {
num[i] = num[i-1] + 1
}
}
for i := size - 2; i >= 0; i-- {
if ratings[i] > ratings[i+1] {
num[i] = max(num[i+1]+1, num[i])
}
}
result := 0
for i := 0; i < size; i++ {
result += num[i]
}
return result
}