Count Positions on Street With Required Brightness - Problem

Imagine you're a city planner working on optimizing street lighting for a perfectly straight road. The street is represented by positions from 0 to n-1, and you have several street lamps installed at various positions.

Here's the challenge: Each street lamp at position i with range r illuminates all positions from max(0, i-r) to min(n-1, i+r). The brightness of any position is simply the number of street lamps that light it up.

You're given a requirement array where requirement[i] specifies the minimum brightness needed at position i. Your task is to count how many positions meet their brightness requirements.

For example, if position 3 needs at least 2 lamps to light it up, and currently 3 lamps illuminate that position, then position 3 satisfies its requirement.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5, lights = [[0,1],[2,1],[3,2]], requirement = [0,2,1,4,1]
โ€บ Output: 4
๐Ÿ’ก Note: Position 0: brightness=1 (lamp at 0), requirement=0 โœ“. Position 1: brightness=2 (lamps at 0,2), requirement=2 โœ“. Position 2: brightness=2 (lamps at 2,3), requirement=1 โœ“. Position 3: brightness=2 (lamps at 2,3), requirement=4 โœ—. Position 4: brightness=1 (lamp at 3), requirement=1 โœ“. Total: 4 positions meet requirements.
example_2.py โ€” All Requirements Met
$ Input: n = 3, lights = [[1,1]], requirement = [1,1,1]
โ€บ Output: 3
๐Ÿ’ก Note: Single lamp at position 1 with range 1 lights up all positions 0,1,2. All positions have brightness=1 and requirement=1, so all 3 positions satisfy their requirements.
example_3.py โ€” No Requirements Met
$ Input: n = 4, lights = [[1,0],[3,0]], requirement = [2,2,2,2]
โ€บ Output: 0
๐Ÿ’ก Note: Two lamps with range 0 only light their own positions. Each position has brightness โ‰ค 1, but all require brightness โ‰ฅ 2. No position meets its requirement.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค lights.length โ‰ค 105
  • lights[i].length == 2
  • 0 โ‰ค positioni < n
  • 0 โ‰ค rangei โ‰ค 109
  • requirement.length == n
  • 0 โ‰ค requirement[i] โ‰ค 105

Visualization

Tap to expand
Street (positions 0 to n-1)Lamp 1Lamp 2Lamp 3Brightness = Number of overlapping lamp rangesGoal: Count positions where brightness โ‰ฅ requirementOverlapping area (high brightness)
Understanding the Visualization
1
Place Lamps
Each lamp at position p with range r lights [p-r, p+r]
2
Calculate Brightness
Count overlapping lamp coverage at each position
3
Check Requirements
Compare brightness vs minimum requirement at each position
Key Takeaway
๐ŸŽฏ Key Insight: Use difference arrays to avoid recalculating lamp coverage for each position - mark range boundaries once, then accumulate with prefix sum!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.4K Views
Medium Frequency
~18 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen