[LeetCode] Count Positions on Street With Required Brightness

2237. Count Positions on Street With Required Brightness

You are given an integer n. A perfectly straight street is represented by a number line ranging from 0 to n - 1. You are given a 2D integer array lights representing the street lamp(s) on the street. Each lights[i] = [positioni, rangei] indicates that there is a street lamp at position positioni that lights up the area from [max(0, positioni - rangei), min(n - 1, positioni + rangei)] (inclusive).

The brightness of a position p is defined as the number of street lamps that light up the position p. You are given a 0-indexed integer array requirement of size n where requirement[i] is the minimum brightness of the ith position on the street.

Return the number of positions i on the street between 0 and n - 1 that have a brightness of at least requirement[i].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
vector<int> psum(n + 1);
for(auto& light : lights) {
int mid = light[0], len = light[1];
int l = max(0,mid-len), r = min(n,mid + len + 1);
psum[l] += 1;
psum[r] -= 1;
}
int res = 0;
for(int i = 0; i < n; i++) {
psum[i] = (i ? psum[i-1] : 0) + psum[i];
res += (psum[i] >= requirement[i]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/01/PS/LeetCode/count-positions-on-street-with-required-brightness/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.