Maximum Walls Destroyed by Robots

There is an endless straight line populated with some robots and walls. You are given integer arrays robots, distance, and walls:

  • robots[i] is the position of the i^th robot.
  • distance[i] is the maximum distance the i^th robot's bullet can travel.
  • walls[j] is the position of the j^th wall.

Every robot has one bullet that can either fire to the left or the right at most distance[i] meters.

A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it immediately stops at that robot and cannot continue.

Return the maximum number of unique walls that can be destroyed by the robots.

Notes:

  • A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.
  • Robots are not destroyed by bullets.
Example 1
Inputrobots = [4], distance = [3], walls = [1,10]
Output1
The robot at position 4 fires left with distance 3, covering [1, 4] and destroying the wall at position 1.
Example 2
Inputrobots = [10,2], distance = [5,1], walls = [5,2,7]
Output3
The robot at position 10 destroys walls at positions 5 and 7 by firing left, and the robot at position 2 destroys the wall at position 2 by firing left.

Constraints

  • 1 <= robots.length == distance.length <= 10^5
  • 1 <= walls.length <= 10^5
  • 1 <= robots[i], walls[j] <= 10^9
  • 1 <= distance[i] <= 10^5
  • All values in robots are unique
  • All values in walls are unique

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate