Maximum Walls Destroyed by Robots Problem
Description
LeetCode Problem 3661.
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 thei^throbot.distance[i]is the maximum distance thei^throbot's bullet can travel.walls[j]is the position of thej^thwall.
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:
Input: robots = [4], distance = [3], walls = [1,10]
Output: 1
Explanation:
- robots[0] = 4 fires left with distance[0] = 3, covering [1, 4] and destroys walls[0] = 1.
- Thus, the answer is 1.
Example 2:
Input: robots = [10,2], distance = [5,1], walls = [5,2,7]
Output: 3
Explanation:
- robots[0] = 10 fires left with distance[0] = 5, covering [5, 10] and destroys walls[0] = 5 and walls[2] = 7.
- robots[1] = 2 fires left with distance[1] = 1, covering [1, 2] and destroys walls[1] = 2.
- Thus, the answer is 3.
Example 3:
Input: robots = [1,2], distance = [100,1], walls = [10]
Output: 0
Explanation:
In this example, only robots[0] can reach the wall, but its shot to the right is blocked by robots[1]; thus the answer is 0.
Constraints:
1 <= robots.length == distance.length <= 10^51 <= walls.length <= 10^51 <= robots[i], walls[j] <= 10^91 <= distance[i] <= 10^5- All values in
robotsare unique - All values in
wallsare unique
Solution
Approach: Dynamic Programming
Sort the robots by position (keeping each robot's distance) and sort the walls. A bullet stops at the next robot, so a robot's left shot covers [max(x - d, previous robot + 1), x] and its right shot covers [x, min(x + d, next robot - 1)] (a wall on a robot's own position belongs to that robot). The only overlap between robots is a right shot of robot i and a left shot of robot i + 1 in the gap between them.
Let f[i][j] be the most walls destroyed by robots 0..i, where j = 0 means robot i + 1 fires left and j = 1 means it fires right. If robot i fires left, add the walls in its left range to f[i - 1][0]. If it fires right, add the walls in its right range to f[i - 1][1]; when j = 0 the right range is cut at the left end of robot i + 1's shot, so walls counted twice are avoided. The answer is f[n - 1][1]. Counting walls in a range is two binary searches.
Complexity: time O(n log n + m log m) for sorting plus O((n) log m) for the searches, space O(n), where n is the number of robots and m the number of walls.
from bisect import bisect_left
class Solution:
def maxWalls(self, robots: List[int], distance: List[int], walls: List[int]) -> int:
n = len(robots)
arr = sorted(zip(robots, distance))
walls.sort()
# f[i][j]: best for robots 0..i; j = 0 if robot i+1 fires left, 1 if it fires right
f = [[0, 0] for _ in range(n)]
for i in range(n):
x, d = arr[i]
left = x - d
if i > 0:
left = max(left, arr[i - 1][0] + 1)
cnt_left = bisect_left(walls, x + 1) - bisect_left(walls, left)
for j in range(2):
right = x + d
if i + 1 < n:
if j == 0:
right = min(right, arr[i + 1][0] - arr[i + 1][1] - 1)
else:
right = min(right, arr[i + 1][0] - 1)
cnt_right = bisect_left(walls, right + 1) - bisect_left(walls, x)
p0, p1 = (f[i - 1][0], f[i - 1][1]) if i > 0 else (0, 0)
f[i][j] = max(p0 + cnt_left, p1 + cnt_right)
return f[n - 1][1]