Skip to content

2237. Count Positions on Street With Required Brightness 👍

  • Time: $O(n + |\texttt{lights}|)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
 public:
  int meetRequirement(int n, vector<vector<int>>& lights,
                      vector<int>& requirement) {
    int ans = 0;
    int currBrightness = 0;
    vector<int> change(n + 1);

    for (const vector<int>& light : lights) {
      const int position = light[0];
      const int range = light[1];
      ++change[max(0, position - range)];
      --change[min(n, position + range + 1)];
    }

    for (int i = 0; i < n; ++i) {
      currBrightness += change[i];
      if (currBrightness >= requirement[i])
        ++ans;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
  public int meetRequirement(int n, int[][] lights, int[] requirement) {
    int ans = 0;
    int currBrightness = 0;
    int[] change = new int[n + 1];

    for (int[] light : lights) {
      final int position = light[0];
      final int range = light[1];
      ++change[Math.max(0, position - range)];
      --change[Math.min(n, position + range + 1)];
    }

    for (int i = 0; i < n; ++i) {
      currBrightness += change[i];
      if (currBrightness >= requirement[i])
        ++ans;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
  def meetRequirement(
      self,
      n: int,
      lights: list[list[int]],
      requirement: list[int],
  ) -> int:
    ans = 0
    currBrightness = 0
    change = [0] * (n + 1)

    for position, rg in lights:
      change[max(0, position - rg)] += 1
      change[min(n, position + rg + 1)] -= 1

    for i in range(n):
      currBrightness += change[i]
      if currBrightness >= requirement[i]:
        ans += 1

    return ans