LeetCode 1176: Diet Plan Performance Solution

Master LeetCode problem 1176 (Diet Plan Performance), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1176. Diet Plan Performance

Problem Explanation

Explanation:

To solve this problem, we need to calculate the total points based on the given diet plan and the calories consumed. The points are calculated based on the following rules:

  • For each day i, if the total calories consumed on that day is less than lower, the total points decrease by 1.
  • For each day i, if the total calories consumed on that day is greater than upper, the total points increase by 1.
  • Otherwise, the total points remain the same.

We can iterate through the calories array and calculate the total calories consumed in a sliding window of k days. Then, for each window, we can apply the rules mentioned above to calculate the total points.

: :

Solution Code

class Solution {
    public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
        int totalPoints = 0;
        int totalCalories = 0;
        
        for (int i = 0; i < calories.length; i++) {
            totalCalories += calories[i];
            if (i >= k - 1) {
                if (i >= k) {
                    totalCalories -= calories[i - k];
                }
                if (totalCalories < lower) {
                    totalPoints--;
                } else if (totalCalories > upper) {
                    totalPoints++;
                }
            }
        }
        
        return totalPoints;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1176 (Diet Plan Performance)?

This page provides optimized solutions for LeetCode problem 1176 (Diet Plan Performance) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1176 (Diet Plan Performance)?

The time complexity for LeetCode 1176 (Diet Plan Performance) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1176 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1176 in Java, C++, or Python.

Back to LeetCode Solutions