LeetCode 152 Race Course Week

Second, the fitness program evaluation ( LeetCode 5174- )

2.1 Title Description

2.2 Problem-solving ideas

Very simple a question, a clear understanding of the meaning of problems just fine. As long as k consecutive days can be.

2.3 Code Solving


public class Solution {

    public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {

        int res = 0;
        int len = calories.length;
        OK:
        for (int i = 0; i < len; i++) {
            int sum = 0;
            if (i + k > len) {
                continue OK;
            }
            for (int j = 0; j < k; j++) {
                sum += calories[i + j];
            }
            if (sum > upper) {
                res++;
            }
            if (sum < lower) {
                res--;
            }

        }
        return res;
    }

}

Guess you like

Origin www.cnblogs.com/fonxian/p/11444302.html