Count Positions on Street With Required Brightness - Problem

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

Input & Output

Example 1 — Basic Case
$ Input: n = 5, lights = [[0,1],[2,1],[3,2]], requirement = [2,2,2,2,2]
Output: 2
💡 Note: Position 0: covered by lamp [0,1] → brightness = 1 < 2. Position 1: covered by lamps [0,1] and [2,1] → brightness = 2 ≥ 2. Position 2: covered by lamps [2,1] and [3,2] → brightness = 2 ≥ 2. Position 3: covered by lamps [2,1] and [3,2] → brightness = 2 ≥ 2. Position 4: covered by lamp [3,2] → brightness = 1 < 2. Only positions 1 and 3 meet requirements.
Example 2 — All Satisfied
$ Input: n = 3, lights = [[1,2]], requirement = [1,1,1]
Output: 3
💡 Note: Single lamp at position 1 with range 2 covers positions [max(0,1-2), min(3-1,1+2)] = [0,2]. All positions have brightness = 1 ≥ 1.
Example 3 — High Requirements
$ Input: n = 4, lights = [[0,1],[1,1]], requirement = [1,2,1,1]
Output: 3
💡 Note: Lamp [0,1] covers [0,1]. Lamp [1,1] covers [0,2]. Position 0: brightness = 2 ≥ 1. Position 1: brightness = 2 ≥ 2. Position 2: brightness = 1 ≥ 1. Position 3: brightness = 0 < 1. Three positions satisfy requirements.

Constraints

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

Visualization

Tap to expand
Street Lighting Problem OverviewStreet positions:01234Lamp [0,1]Lamp [2,1]Lamp [3,2]Requirements: [2,2,2,2,2]1 < 22 ≥ 22 ≥ 22 ≥ 21 < 2Result: 3 positions satisfy requirements
Understanding the Visualization
1
Input
Street positions 0 to n-1, lamps with position and range
2
Coverage
Each lamp illuminates a range of positions
3
Count
Count positions where brightness ≥ requirement
Key Takeaway
🎯 Key Insight: Use difference array to efficiently calculate brightness at all positions with range updates
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~25 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