robots, distance, and walls:robots[i] is the position of the ith robot.distance[i] is the maximum distance the ith robot's bullet can travel.walls[j] is the position of the jth 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:
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.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.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 <= 1051 <= walls.length <= 1051 <= robots[i], walls[j] <= 1091 <= distance[i] <= 105robots are uniquewalls are uniqueWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the maximum number of unique walls destroyed by robots that can each fire either left or right without shooting past adjacent robots. By sorting robots and walls, we can determine the exact segment covered by firing in either direction for every robot. A dynamic programming approach then computes the optimal sequence of choices by resolving overlaps only when adjacent robots fire towards each other.
Here's how the algorithm would work step-by-step:
import bisect
def maximum_walls_destroyed(robots, distance, walls):
# Pair each robot with its firing distance and sort them by position.
robot_pairs = sorted(zip(robots, distance))
sorted_walls = sorted(walls)
total_robots = len(robot_pairs)
left_ranges = []
right_ranges = []
# Determine the effective firing intervals for each robot in both directions.
for index, (position, dist) in enumerate(robot_pairs):
min_left = position - dist
if index > 0:
min_left = max(min_left, robot_pairs[index - 1][0] + 1)
left_ranges.append((min_left, position))
max_right = position + dist
if index + 1 < total_robots:
max_right = min(max_right, robot_pairs[index + 1][0] - 1)
right_ranges.append((position, max_right))
def count_walls(start_pos, end_pos):
if start_pos > end_pos:
return 0
left_idx = bisect.bisect_left(sorted_walls, start_pos)
right_idx = bisect.bisect_right(sorted_walls, end_pos)
return right_idx - left_idx
# Use dynamic programming to track the maximum destroyed walls choosing left or right shots.
dp_left = count_walls(left_ranges[0][0], left_ranges[0][1])
dp_right = count_walls(right_ranges[0][0], right_ranges[0][1])
for index in range(1, total_robots):
curr_left_walls = count_walls(left_ranges[index][0], left_ranges[index][1])
curr_right_walls = count_walls(right_ranges[index][0], right_ranges[index][1])
prev_right_start, prev_right_end = right_ranges[index - 1]
curr_left_start, curr_left_end = left_ranges[index]
overlap_start = max(prev_right_start, curr_left_start)
overlap_end = min(prev_right_end, curr_left_end)
overlap_count = count_walls(overlap_start, overlap_end)
next_dp_left = max(dp_left + curr_left_walls, dp_right + curr_left_walls - overlap_count)
next_dp_right = max(dp_left + curr_right_walls, dp_right + curr_right_walls)
dp_left, dp_right = next_dp_left, next_dp_right
return max(dp_left, dp_right)
Instead of testing every single way the robots could shoot, the optimal approach organizes the positions from left to right to map out clear boundaries. By examining how each robot can reach outwards without overlapping with its neighbors, we can quickly calculate the maximum impact for every robot.
Here's how the algorithm would work step-by-step:
def max_walls_destroyed(robot_positions, robot_strengths, wall_positions):
sorted_robots = sorted(zip(robot_positions, robot_strengths))
sorted_walls = sorted(wall_positions)
# Sorting allows us to establish clear spatial boundaries between adjacent robots.
robot_intervals = []
for robot_index, (robot_pos, robot_str) in enumerate(sorted_robots):
left_bound = sorted_robots[robot_index - 1][0] if robot_index > 0 else -float('inf')
right_bound = sorted_robots[robot_index + 1][0] if robot_index < len(sorted_robots) - 1 else float('inf')
effective_left = max(left_bound, robot_pos - robot_str)
effective_right = min(right_bound, robot_pos + robot_str)
robot_intervals.append((effective_left, effective_right))
# We map out unique wall destruction zones to prevent double counting shared targets.
unique_walls_destroyed = set()
for left_limit, right_limit in robot_intervals:
for wall_pos in sorted_walls:
if left_limit <= wall_pos <= right_limit:
unique_walls_destroyed.add(wall_pos)
# Summing the unique contributions guarantees accurate total destruction count.
total_walls_destroyed = len(unique_walls_destroyed)
return total_walls_destroyed
| Case | How to Handle |
|---|---|
| Empty robot or wall inputs | Return zero immediately as no walls can be destroyed without active robots or targets. |
| Negative coordinates for robots and walls | Sort positions and ranges using standard numerical comparisons which correctly handle negative values. |
| Overlapping ranges from multiple robots | Use a set or interval merging technique to ensure each unique wall is only counted once regardless of how many robots target it. |
| Robots with zero shooting range | Treat them as single-point targets that can only destroy a wall if they occupy the exact same coordinate. |
| Extremely large coordinates leading to integer overflow during range calculations | Use 64-bit integers for position arithmetic to prevent overflow when summing ranges and coordinates. |
| Massive scale with maximum number of robots and walls | Employ an efficient sorting and two-pointer or sweep-line algorithm to ensure time complexity stays within logarithmic scaling limits. |
| Robots positioned such that left and right firing paths overlap with each other | Model the decision for each robot as a dynamic programming state or interval scheduling problem to avoid double-counting. |
| All walls falling entirely outside the maximum possible range of any robot | Boundary checks during the interval matching phase will yield zero intersections and correctly return a total count of zero. |