Diet Plan Performance

Problem Id: 1176 Difficulty: Easy


Intuition

Solution


class Solution:
    def dietPlanPerformance(self, calories: List[int], k: int, lower: int, upper: int) -> int:
        score = 0
        window = 0
        for i in range(k):
            window += calories[i]
        if window < lower:
            score -= 1
        elif window > upper:
            score += 1

        for i in range(k, len(calories)):
            window += calories[i]
            window -= calories[i - k]
            if window < lower:
                score -= 1
            elif window > upper:
                score += 1
        return score