2 minutes
Leetcode 1176
This is how the code works:
- We sum up the first
k
elements. - Then compare whether sum is greater than upper or smaller than lower.
- After comparing we can loop from
k
tolen(calories)
. - Then we can take off the beggining of the subarray by subtracting
calories[i - k]
. And then we have to add the next element from the end of the subarray to the end to the subarray, we do this by addingcalories[i]
. We can dosum -= calories[i - k]
, and thensum += calories[i]
, or we can simplify these two lines into one line,sum -= calories[i - k] - calories[i]
orsum += calories[i] - calories[i - k]
. - Then compare whether sum is greater than upper or smaller than lower.
The Code:
func dietPlanPerformance(calories []int, k int, lower int, upper int) int {
sum := 0
res := 0
for i := 0; i < k; i++ {
sum += calories[i]
}
res += comparison(sum, lower, upper)
for i := k; i < len(calories); i++ {
sum -= calories[i - k] - calories[i]
res += comparison(sum, lower, upper)
}
return res
}
func comparison(sum int, lower int, upper int) int {
if sum < lower {
return -1
} else if sum > upper {
return 1
}
return 0
}
Read other posts