-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (57 loc) · 1.08 KB
/
main.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
package main
// brute force
func dietPlanPerformance(calories []int, k, lower, upper int) int {
var ans int
for pnt1 := 0; pnt1 < len(calories); pnt1++ {
pnt2 := pnt1 + k
if pnt2 <= len(calories) {
var consume int
for i := pnt1; i < pnt2; i++ {
consume += calories[i]
}
if consume < lower {
ans--
}
if consume > upper {
ans++
}
}
}
return ans
}
func dietPlanPerformance2(calories []int, k, lower, upper int) int {
prefixSum, prev := make([]int, len(calories)), 0
for i := 0; i < len(calories); i++ {
prefixSum[i] = calories[i] + prev
prev = prefixSum[i]
}
ans, prev := 0, 0
for i := k; i <= len(prefixSum); i++ {
if prefixSum[i-1]-prev < lower {
ans--
}
if prefixSum[i-1]-prev > upper {
ans++
}
prev = prefixSum[i-k]
}
return ans
}
func dietPlanPerformance3(calories []int, k, lower, upper int) int {
ans, curr := 0, 0
for i := 0; i < len(calories); i++ {
curr += calories[i]
if i-k >= 0 {
curr -= calories[i-k]
}
if i+1-k >= 0 {
if curr < lower {
ans--
}
if curr > upper {
ans++
}
}
}
return ans
}