返回介绍

solution / 2200-2299 / 2237.Count Positions on Street With Required Brightness / README_EN

发布于 2024-06-17 01:03:08 字数 5799 浏览 0 评论 0 收藏 0

2237. Count Positions on Street With Required Brightness

中文文档

Description

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]_._

 

Example 1:

Input: n = 5, lights = [[0,1],[2,1],[3,2]], requirement = [0,2,1,4,1]
Output: 4
Explanation:
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 1] (inclusive).
- The second street lamp lights up the area from [max(0, 2 - 1), min(n - 1, 2 + 1)] = [1, 3] (inclusive).
- The third street lamp lights up the area from [max(0, 3 - 2), min(n - 1, 3 + 2)] = [1, 4] (inclusive).

-   Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is greater than requirement[0].
-   Position 1 is covered by the first, second, and third street lamps. It is covered by 3 street lamps which is greater than requirement[1].
-   Position 2 is covered by the second and third street lamps. It is covered by 2 street lamps which is greater than requirement[2].
-   Position 3 is covered by the second and third street lamps. It is covered by 2 street lamps which is less than requirement[3].
-   Position 4 is covered by the third street lamp. It is covered by 1 street lamp which is equal to requirement[4].

Positions 0, 1, 2, and 4 meet the requirement so we return 4.

Example 2:

Input: n = 1, lights = [[0,1]], requirement = [2]
Output: 0
Explanation:
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 0] (inclusive).
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is less than requirement[0].
- We return 0 because no position meets their brightness requirement.

 

Constraints:

  • 1 <= n <= 105
  • 1 <= lights.length <= 105
  • 0 <= positioni < n
  • 0 <= rangei <= 105
  • requirement.length == n
  • 0 <= requirement[i] <= 105

Solutions

Solution 1

class Solution:
  def meetRequirement(
    self, n: int, lights: List[List[int]], requirement: List[int]
  ) -> int:
    d = [0] * 100010
    for p, r in lights:
      i, j = max(0, p - r), min(n - 1, p + r)
      d[i] += 1
      d[j + 1] -= 1
    return sum(s >= r for s, r in zip(accumulate(d), requirement))
class Solution {
  public int meetRequirement(int n, int[][] lights, int[] requirement) {
    int[] d = new int[100010];
    for (int[] e : lights) {
      int i = Math.max(0, e[0] - e[1]);
      int j = Math.min(n - 1, e[0] + e[1]);
      ++d[i];
      --d[j + 1];
    }
    int s = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      s += d[i];
      if (s >= requirement[i]) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int meetRequirement(int n, vector<vector<int>>& lights, vector<int>& requirement) {
    vector<int> d(100010);
    for (auto& e : lights) {
      int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]);
      ++d[i];
      --d[j + 1];
    }
    int s = 0, ans = 0;
    for (int i = 0; i < n; ++i) {
      s += d[i];
      if (s >= requirement[i]) ++ans;
    }
    return ans;
  }
};
func meetRequirement(n int, lights [][]int, requirement []int) int {
  d := make([]int, 100010)
  for _, e := range lights {
    i, j := max(0, e[0]-e[1]), min(n-1, e[0]+e[1])
    d[i]++
    d[j+1]--
  }
  var s, ans int
  for i, r := range requirement {
    s += d[i]
    if s >= r {
      ans++
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文