Diet Plan Performance

看着复杂的一道题, 就是比较k个连续数的大小. 滑窗就可以了.

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