Algorithm
Maximum Walls Destroyed by Robots
Solution
for i in range(n):
robot_pos, robot_dist_val = robot_dist[i]
left_bound = max(robot_pos - robot_dist_val, robot_dist[i-1][0] + 1) if i > 0 else robot_pos - robot_dist_val
right_bound = min(robot_pos + robot_dist_val, robot_dist[i+1][0] - 1) if i < n - 1 else robot_pos + robot_dist_val
while right_ptr < m and walls[right_ptr] <= right_bound: right_ptr += 1
while cur_ptr < m and walls[cur_ptr] <= robot_pos: cur_ptr += 1
while left_ptr < m and walls[left_ptr] < left_bound: left_ptr += 1
while robot_ptr < m and (i == 0 or walls[robot_ptr] <= robot_dist[i-1][0]): robot_ptr += 1Video GuideLeetcode Daily
Time Complexity
O(N log N + M log M)
Space Complexity
O(N + log M)
