A dieter consumes calories[i]
calories on the i
-th day.
Given an integer k
, for every consecutive sequence of k
days (calories[i], calories[i+1], ..., calories[i+k-1]
for all 0 <= i <= n-k
), they look at T, the total calories consumed during that sequence of k
days (calories[i] + calories[i+1] + ... + calories[i+k-1]
):
T < lower
, they performed poorly on their diet and lose 1 point;T > upper
, they performed well on their diet and gain 1 point;Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length
days.
Note that the total points can be negative.
Example 1:
Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3 Output: 0 Explanation: Since k = 1, we consider each element of the array separately and compare it to lower and upper. calories[0] and calories[1] are less than lower so 2 points are lost. calories[3] and calories[4] are greater than upper so 2 points are gained.
Example 2:
Input: calories = [3,2], k = 2, lower = 0, upper = 1 Output: 1 Explanation: Since k = 2, we consider subarrays of length 2. calories[0] + calories[1] > upper so 1 point is gained.
Example 3:
Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5 Output: 0 Explanation: calories[0] + calories[1] > upper so 1 point is gained. lower <= calories[1] + calories[2] <= upper so no change in points. calories[2] + calories[3] < lower so 1 point is lost.
Constraints:
1 <= k <= calories.length <= 10^5
0 <= calories[i] <= 20000
0 <= lower <= upper
program main
implicit none
integer :: i, j, k, n, lower, upper, points, total_points
integer, dimension(:), allocatable :: calories
! read input
read(*,*) n
allocate(calories(n))
do i=1,n
read(*,*) calories(i)
end do
read(*,*) k
read(*,*) lower
read(*,*) upper
! solve problem
points = 0
do i=1,n-k+1
total_points = 0
do j=i,i+k-1
total_points = total_points + calories(j)
end do
if (total_points < lower) then
points = points - 1
else if (total_points > upper) then
points = points + 1
end if
end do
! print output
write(*,*) points
end program main
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b40d5f4a960 in ??? #1 0x7b40d5f4b4d9 in ??? #2 0x7b40d619f17b in ??? #3 0x7b40d6198684 in ??? #4 0x7b40d61992aa in ??? #5 0x5a8effd97272 in MAIN__ #6 0x5a8effd976df in main
module diet
implicit none
contains
function solve(calories, k, lower, upper) result(points)
integer, intent(in) :: calories(:), k, lower, upper
integer :: points
integer :: i, j, T
points = 0
do i = 1, size(calories) - k + 1
T = sum(calories(i:i+k-1))
if (T < lower) then
points = points - 1
else if (T > upper) then
points = points + 1
end if
end do
end function solve
end module diet
program test
use diet
implicit none
integer, parameter :: n = 5
integer, parameter :: k = 1
integer, parameter :: lower = 3
integer, parameter :: upper = 3
integer :: calories(n) = [1, 2, 3, 4, 5]
integer :: points
points = solve(calories, k, lower, upper)
write (*,*) "Total points:", points
calories = [3, 2]
points = solve(calories, k, lower, upper)
write (*,*) "Total points:", points
calories = [6, 5, 0, 0]
points = solve(calories, k, lower, upper)
write (*,*) "Total points:", points
end program test
temp.f95:34:2: 34 | calories = [3, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 2) temp.f95:38:2: 38 | calories = [6, 5, 0, 0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 4)
def dietPlanPerformance(calories, k, lower, upper):
points = 0
T = 0
for i in range(len(calories)):
T += calories[i]
if i >= k:
T -= calories[i-k]
if i >= k-1:
if T < lower: points -= 1
if T > upper: points += 1
return points
T
to 0.T
.k
. If so, subtract the calories of the first day of the previous window from T
.k
. If so, perform the point calculation:T
is less than lower
, decrement the points.T
is greater than upper
, increment the points.int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {
int points = 0;
int T = 0;
for(int i = 0; i < calories.size(); i++) {
T += calories[i];
if(i >= k) {
T -= calories[i-k];
}
if(i >= k-1) {
if(T < lower) points--;
if(T > upper) points++;
}
}
return points;
}
T
to 0.T
.k
. If so, subtract the calories of the first day of the previous window from T
.k
. If so, perform the point calculation:T
is less than lower
, decrement the points.T
is greater than upper
, increment the points.