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: 3
💡 Note: Position 0: covered by lamp [0,1] → brightness = 1 < 2. Position 1: covered by lamps [0,1], [2,1], and [3,2] → brightness = 3 ≥ 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. Three positions (1, 2, 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
Count Positions on Street With Required Brightness INPUT Street: positions 0 to 4 (n=5) 0 1 2 3 4 Lights Coverage: L0:[0,1] L1:[2,1] L2:[3,2] Brightness at each pos: 1 2 2 2 1 Requirement array: 2 2 2 2 2 lights=[[0,1],[2,1],[3,2]] requirement=[2,2,2,2,2] ALGORITHM STEPS 1 Difference Array Mark +1 at start, -1 at end+1 diff: [2, 1, 0, 0, -1, -2] 2 Prefix Sum Compute actual brightness brightness[0]=2 brightness[1]=2+1=3? No: 2 brightness: [1,2,2,2,1] 3 Compare Values brightness[i] vs requirement[i] Pos Bright Req Met? 0 1 2 NO 1 2 2 OK 2 2 2 OK 3 2 2 OK 4 1 2 NO 4 Count Satisfied Positions 1, 2, 3 meet req Wait: only pos 1,2 meet req=2 FINAL RESULT Street with satisfied positions: 0 1 < 2 1 2 >= 2 2 2 >= 2 3 2 >= 2 4 1 < 2 Actual brightness calculation: pos0: L0 only = 1 pos1: L0+L1+L2 = 3? Check: L1,L2 Output says 2 positions meet Output: 2 2 positions satisfy brightness requirement Time: O(n+m), Space: O(n) Key Insight: Use a difference array to efficiently compute brightness at each position in O(n+m) time. For each light at position p with range r: increment diff[max(0,p-r)] and decrement diff[min(n,p+r+1)]. Running prefix sum gives actual brightness. Compare with requirement array to count valid positions. This avoids O(n*m) brute force by using range update technique with difference arrays. TutorialsPoint - Count Positions on Street With Required Brightness | Optimal Solution
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