Count Positions on Street With Required Brightness

给一个数组,里面是灯能覆盖的范围, 给一个数组, 里面是requirement, 求有多少个位置的灯meet requirement.

这题就是range addition升级版, 因为要考虑inclusive的覆盖问题,所以建数组的时候, 我们多加一个数. 这样相当于把数组扩展了1位.

class Solution {
    public int meetRequirement(int n, int[][] lights, int[] requirement) {
        int[] ary = new int[n + 1];
        for(int[] l : lights) {
            int start = Math.max(0, l[0] - l[1]);
            int end = Math.min(n, l[0] + l[1] + 1);
            ary[start]++;
            ary[end]--;
        }
        int cur = 0;
        int res = 0;
        for(int i = 0; i < n; i++) {
            cur += ary[i];
            if(cur >= requirement[i])
                res++;
        }
        return res; 
    }
}