Hard

题目描述

有一条无限长的直线上有一些机器人和墙。给你整数数组 robotsdistancewalls

  • robots[i] 是第 i 个机器人的位置。
  • distance[i] 是第 i 个机器人的子弹最远能移动的距离。
  • walls[j] 是第 j 面墙的位置。

每个机器人有一颗子弹,可以向左或向右发射,最远距离为 distance[i] 米。

子弹会摧毁路径上在射程范围内的每面墙。机器人是固定障碍物:如果子弹在到达墙之前撞到另一个机器人,它会立即在该机器人处停止,无法继续前进。

返回机器人能摧毁的不同墙的最大数量。

注意:

  • 墙和机器人可能在同一位置;该位置的墙可以被机器人摧毁。
  • 机器人不会被子弹摧毁。

示例 1:

输入:robots = [4], distance = [3], walls = [1,10]
输出:1
解释:
robots[0] = 4 向左发射,距离为 distance[0] = 3,覆盖范围 [1, 4],摧毁 walls[0] = 1。
因此答案是 1。

示例 2:

输入:robots = [10,2], distance = [5,1], walls = [5,2,7]
输出:3
解释:
robots[0] = 10 向左发射,距离为 distance[0] = 5,覆盖范围 [5, 10],摧毁 walls[0] = 5 和 walls[2] = 7。
robots[1] = 2 向左发射,距离为 distance[1] = 1,覆盖范围 [1, 2],摧毁 walls[1] = 2。
因此答案是 3。

示例 3:

输入:robots = [1,2], distance = [100,1], walls = [10]
输出:0
解释:
在这个例子中,只有 robots[0] 能到达墙,但它向右射击被 robots[1] 阻挡;因此答案是 0。

约束:

  • 1 <= robots.length == distance.length <= 10^5
  • 1 <= walls.length <= 10^5
  • 1 <= robots[i], walls[j] <= 10^9
  • 1 <= distance[i] <= 10^5
  • robots 中的所有值都是唯一的
  • walls 中的所有值都是唯一的

解题思路

解题思路

这是一道复杂的动态规划问题,关键在于理解机器人射击的约束条件和状态转移。

核心思路

  1. 排序处理:将机器人和墙都按位置排序,便于计算射击范围和使用二分查找。

  2. 射击约束:机器人可以向左或向右射击,但子弹会被其他机器人阻挡。因此每个机器人的实际射击范围需要考虑相邻机器人的位置限制。

  3. 动态规划状态设计

    • dpLeft[i]:前 i 个机器人中,第 i 个机器人向左射击时能摧毁的最大墙数
    • dpRight[i]:前 i 个机器人中,第 i 个机器人向右射击时能摧毁的最大墙数
  4. 状态转移

    • 向左射击:不会影响前面机器人的射击,可以累加
    • 向右射击:可能会阻挡前面机器人的射击,需要仔细处理重叠区域
  5. 范围计算:使用二分查找快速计算每个射击范围内的墙数量。

算法步骤

  1. 将机器人按位置排序,同时保持距离信息的对应关系
  2. 对墙数组排序
  3. 对每个机器人,计算其向左和向右射击的有效范围(考虑机器人阻挡)
  4. 使用二分查找计算每个范围内的墙数量
  5. 动态规划计算最优解

这个问题的难点在于正确处理机器人之间的阻挡关系和状态转移的设计。

代码实现

class Solution {
public:
    int maxWalls(vector<int>& robots, vector<int>& distance, vector<int>& walls) {
        int n = robots.size();
        vector<pair<int, int>> robotData;
        for (int i = 0; i < n; i++) {
            robotData.push_back({robots[i], distance[i]});
        }
        sort(robotData.begin(), robotData.end());
        sort(walls.begin(), walls.end());
        
        // dpLeft[i]: max walls if robot i shoots left
        // dpRight[i]: max walls if robot i shoots right
        vector<int> dpLeft(n, 0), dpRight(n, 0);
        
        for (int i = 0; i < n; i++) {
            int pos = robotData[i].first;
            int dist = robotData[i].second;
            
            // Calculate left shooting range
            int leftStart = pos - dist;
            int leftEnd = pos;
            if (i > 0) {
                leftStart = max(leftStart, robotData[i-1].first);
            }
            if (leftStart <= leftEnd) {
                int leftWalls = upper_bound(walls.begin(), walls.end(), leftEnd) - 
                               lower_bound(walls.begin(), walls.end(), leftStart);
                dpLeft[i] = leftWalls + (i > 0 ? max(dpLeft[i-1], dpRight[i-1]) : 0);
            } else {
                dpLeft[i] = (i > 0 ? max(dpLeft[i-1], dpRight[i-1]) : 0);
            }
            
            // Calculate right shooting range
            int rightStart = pos;
            int rightEnd = pos + dist;
            if (i < n - 1) {
                rightEnd = min(rightEnd, robotData[i+1].first);
            }
            if (rightStart <= rightEnd) {
                int rightWalls = upper_bound(walls.begin(), walls.end(), rightEnd) - 
                                lower_bound(walls.begin(), walls.end(), rightStart);
                dpRight[i] = rightWalls + (i > 0 ? dpLeft[i-1] : 0);
            } else {
                dpRight[i] = (i > 0 ? dpLeft[i-1] : 0);
            }
        }
        
        return max(dpLeft[n-1], dpRight[n-1]);
    }
};
class Solution:
    def maxWalls(self, robots: List[int], distance: List[int], walls: List[int]) -> int:
        import bisect
        
        n = len(robots)
        robot_data = [(robots[i], distance[i]) for i in range(n)]
        robot_data.sort()
        walls.sort()
        
        # dpLeft[i]: max walls if robot i shoots left
        # dpRight[i]: max walls if robot i shoots right
        dp_left = [0] * n
        dp_right = [0] * n
        
        for i in range(n):
            pos, dist = robot_data[i]
            
            # Calculate left shooting range
            left_start = pos - dist
            left_end = pos
            if i > 0:
                left_start = max(left_start, robot_data[i-1][0])
            
            if left_start <= left_end:
                left_walls = bisect.bisect_right(walls, left_end) - bisect.bisect_left(walls, left_start)
                dp_left[i] = left_walls + (max(dp_left[i-1], dp_right[i-1]) if i > 0 else 0)
            else:
                dp_left[i] = max(dp_left[i-1], dp_right[i-1]) if i > 0 else 0
            
            # Calculate right shooting range
            right_start = pos
            right_end = pos + dist
            if i < n - 1:
                right_end = min(right_end, robot_data[i+1][0])
            
            if right_start <= right_end:
                right_walls = bisect.bisect_right(walls, right_end) - bisect.bisect_left(walls, right_start)
                dp_right[i] = right_walls + (dp_left[i-1] if i > 0 else 0)
            else:
                dp_right[i] = dp_left[i-1] if i > 0 else 0
        
        return max(dp_left[n-1], dp_right[n-1])
public class Solution {
    public int MaxWalls(int[] robots, int[] distance, int[] walls) {
        int n = robots.Length;
        var robotData = new (int pos, int dist)[n];
        for (int i = 0; i < n; i++) {
            robotData[i] = (robots[i], distance[i]);
        }
        Array.Sort(robotData, (a, b) => a.pos.CompareTo(b.pos));
        Array.Sort(walls);
        
        int[] dpLeft = new int[n];
        int[] dpRight = new int[n];
        
        for (int i = 0; i < n; i++) {
            int pos = robotData[i].pos;
            int dist = robotData[i].dist;
            
            // Calculate left shooting range
            int leftStart = pos - dist;
            int leftEnd = pos;
            if (i > 0) {
                leftStart = Math.Max(leftStart, robotData[i-1].pos);
            }
            
            if (leftStart <= leftEnd) {
                int leftWalls = UpperBound(walls, leftEnd) - LowerBound(walls, leftStart);
                dpLeft[i] = leftWalls + (i > 0 ? Math.Max(dpLeft[i-1], dpRight[i-1]) : 0);
            } else {
                dpLeft[i] = i > 0 ? Math.Max(dpLeft[i-1], dpRight[i-1]) : 0;
            }
            
            // Calculate right shooting range
            int rightStart = pos;
            int rightEnd = pos + dist;
            if (i < n - 1) {
                rightEnd = Math.Min(rightEnd, robotData[i+1].pos);
            }
            
            if (rightStart <= rightEnd) {
                int rightWalls = UpperBound(walls, rightEnd) - LowerBound(walls, rightStart);
                dpRight[i] = rightWalls + (i > 0 ? dpLeft[i-1] : 0);
            } else {
                dpRight[i] = i > 0 ? dpLeft[i-1] : 0;
            }
        }
        
        return Math.Max(dpLeft[n-1], dpRight[n-1]);
    }
    
    private int LowerBound(int[] arr, int target) {
        int left = 0, right = arr.Length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] < target) left = mid + 1;
            else right = mid;
        }
        return left;
    }
    
    private int UpperBound(int[] arr, int target) {
        int left = 0, right = arr.Length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] <= target) left = mid + 1;
            else right = mid;
        }
        return left;
    }
}
var maxWalls = function(robots, distance, walls) {
    const n = robots.length;
    const robotData = robots.map((pos, i) => [pos, distance[i]]);
    robotData.sort((a, b) => a[0] - b[0]);
    walls.sort((a, b) => a - b);
    
    const dpLeft = new Array(n).fill(0);
    const dpRight = new Array(n).fill(0);
    
    const lowerBound = (arr, target) => {
        let left = 0, right = arr.length;
        while (left < right) {
            const mid = Math.floor((left + right) / 2);
            if (arr[mid] < target) left = mid + 1;
            else right = mid;
        }
        return left;
    };
    
    const upperBound = (arr, target) => {
        let left = 0, right = arr.length;
        while (left < right) {
            const mid = Math.floor((left + right) / 2);
            if (arr[mid] <= target) left = mid + 1;
            else right = mid;
        }
        return left;
    };
    
    for (let i = 0; i < n; i++) {
        const [pos, dist] = robotData[i];
        
        // Calculate left shooting range
        let leftStart = pos - dist;
        const leftEnd = pos;
        if (i > 0) {
            leftStart = Math.max(leftStart, robotData[i-1][0]);
        }
        
        if (leftStart <= leftEnd) {
            const leftWalls = upperBound(walls, leftEnd) - lowerBound(walls, leftStart);
            dpLeft[i] = leftWalls + (i > 0 ? Math.max(dpLeft[i-1], dpRight[i-1]) : 0);
        } else {
            dpLeft[i] = i > 0 ? Math.max(dpLeft[i-1], dpRight[i-1]) : 0;
        }
        
        // Calculate right shooting range
        const rightStart = pos;
        let rightEnd = pos + dist;
        if (i < n - 1) {
            rightEnd = Math.min(rightEnd, robotData[i+1][0]);
        }
        
        if (rightStart <= rightEnd) {
            const rightWalls = upperBound(walls, rightEnd) - lowerBound(walls, rightStart);
            dpRight[i] = rightWalls + (i > 0 ? dpLeft[i-1] : 0);
        } else {
            dpRight[i] = i > 0 ? dpLeft[i-1] : 0;
        }
    }
    
    return Math.max(dpLeft[n-1], dpRight[n-1]);
};

复杂度分析

复杂度类型复杂度
时间复杂度O(n log n + m log m + n log m)
空间复杂度O(n)

其中 n