Tap here to switch tabs
Problem
Submissions

Problem: Maximum Walls Destroyed by Robots

med
30 min
Try to solve the Maximum Walls Destroyed by Robots problem.

Statement

You are given three integer arrays robots, distance, and walls, all representing positions on a 1D1\text{D} number line.

There are nn robots, where the ithi^{\text{th}} robot is located at position robots[i] and has a maximum firing range of distance[i]. Each robot can choose to fire a laser beam either to the left or to the right:

  • If robot ii fires to the left, the beam covers the interval [max⁡(left_bound,robots[i]−distance[i]),robots[i]][\max(\text{left\_bound}, \text{robots}[i] - \text{distance}[i]), \text{robots}[i]], where left_bound\text{left\_bound} is the position of the nearest robot to its left plus 11, or −∞-\infty if no such robot exists.

  • If robot ii fires to the right, the beam covers the interval [robots[i],min⁡(right_bound,robots[i]+distance[i])][\text{robots}[i], \min(\text{right\_bound}, \text{robots}[i] + \text{distance}[i])], where right_bound\text{right\_bound} is the position of the nearest robot to its right minus 11, or +∞+\infty if no such robot exists.

A laser beam cannot pass through another robot. Any wall located within the covered range of a laser beam is destroyed.

Return the maximum number of unique walls that can be destroyed if each robot independently chooses to fire either to the left or to the right.

Constraints:

  • 1≤1 \leq robots.length == distance.length ≤105\leq 10^5

  • 1≤1 \leq walls.length ≤105\leq 10^5

  • 1≤1 \leq robots[i], walls[j] ≤109\leq 10^9

  • 1≤1 \leq distance[i] ≤105\leq 10^5

  • All values in robots are unique.

  • All values in walls are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Walls Destroyed by Robots

med
30 min
Try to solve the Maximum Walls Destroyed by Robots problem.

Statement

You are given three integer arrays robots, distance, and walls, all representing positions on a 1D1\text{D} number line.

There are nn robots, where the ithi^{\text{th}} robot is located at position robots[i] and has a maximum firing range of distance[i]. Each robot can choose to fire a laser beam either to the left or to the right:

  • If robot ii fires to the left, the beam covers the interval [max⁡(left_bound,robots[i]−distance[i]),robots[i]][\max(\text{left\_bound}, \text{robots}[i] - \text{distance}[i]), \text{robots}[i]], where left_bound\text{left\_bound} is the position of the nearest robot to its left plus 11, or −∞-\infty if no such robot exists.

  • If robot ii fires to the right, the beam covers the interval [robots[i],min⁡(right_bound,robots[i]+distance[i])][\text{robots}[i], \min(\text{right\_bound}, \text{robots}[i] + \text{distance}[i])], where right_bound\text{right\_bound} is the position of the nearest robot to its right minus 11, or +∞+\infty if no such robot exists.

A laser beam cannot pass through another robot. Any wall located within the covered range of a laser beam is destroyed.

Return the maximum number of unique walls that can be destroyed if each robot independently chooses to fire either to the left or to the right.

Constraints:

  • 1≤1 \leq robots.length == distance.length ≤105\leq 10^5

  • 1≤1 \leq walls.length ≤105\leq 10^5

  • 1≤1 \leq robots[i], walls[j] ≤109\leq 10^9

  • 1≤1 \leq distance[i] ≤105\leq 10^5

  • All values in robots are unique.

  • All values in walls are unique.