/**
* Problem: LeetCode 3661 - Maximum Walls Destroyed by Robots
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int maxWalls(int[] robots, int[] distance, int[] walls) {
int n = robots.length;
int[][] r = new int[n][2];
for (int i = 0; i < n; i++) {
r[i][0] = robots[i];
r[i][1] = distance[i];
}
Arrays.sort(r, Comparator.comparingInt(a -> a[0]));
Arrays.sort(walls);
int[] leftBound = new int[n];
int[] rightBound = new int[n];
int[] wLeft = new int[n];
int[] wRight = new int[n];
for (int i = 0; i < n; i++) {
int lLimit = (i > 0) ? r[i - 1][0] + 1 : Integer.MIN_VALUE;
leftBound[i] = Math.max(r[i][0] - r[i][1], lLimit);
wLeft[i] = countWalls(walls, leftBound[i], r[i][0]);
int rLimit = (i < n - 1) ? r[i + 1][0] - 1 : Integer.MAX_VALUE;
rightBound[i] = Math.min(r[i][0] + r[i][1], rLimit);
wRight[i] = countWalls(walls, r[i][0], rightBound[i]);
}
// dp0: robot i fires Left
// dp1: robot i fires Right
int dp0 = wLeft[0];
int dp1 = wRight[0];
for (int i = 1; i < n; i++) {
int wBoth;
if (rightBound[i - 1] >= leftBound[i]) {
wBoth = countWalls(walls, r[i - 1][0], r[i][0]);
} else {
wBoth = wRight[i - 1] + wLeft[i];
}
int nextDp0 = Math.max(dp0 + wLeft[i], dp1 - wRight[i - 1] + wBoth);
int nextDp1 = Math.max(dp0, dp1) + wRight[i];
dp0 = nextDp0;
dp1 = nextDp1;
}
return Math.max(dp0, dp1);
}
private int countWalls(int[] walls, int low, int high) {
if (low > high) {
return 0;
}
int start = lowerBound(walls, low);
int end = upperBound(walls, high);
return Math.max(0, end - start);
}
private int lowerBound(int[] arr, int target) {
int low = 0, high = arr.length;
while (low < high) {
int mid = (low + high) >>> 1;
if (arr[mid] >= target) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
private int upperBound(int[] arr, int target) {
int low = 0, high = arr.length;
while (low < high) {
int mid = (low + high) >>> 1;
if (arr[mid] > target) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
}
"""
Problem: LeetCode 3661 - Maximum Walls Destroyed by Robots
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from bisect import bisect_left, bisect_right
from typing import List
class Solution:
def maxWalls(self, robots: List[int], distance: List[int], walls: List[int]) -> int:
"""
Calculates the maximum number of unique walls destroyed by robots.
Time Complexity: O(N log N + M log M + N log M)
Space Complexity: O(N)
"""
n = len(robots)
r = sorted(zip(robots, distance), key=lambda x: x[0])
walls.sort()
def count_walls(low: int, high: int) -> int:
if low > high:
return 0
start = bisect_left(walls, low)
end = bisect_right(walls, high)
return max(0, end - start)
left_bound = [0] * n
right_bound = [0] * n
w_left = [0] * n
w_right = [0] * n
for i in range(n):
l_limit = r[i - 1][0] + 1 if i > 0 else -float("inf")
left_bound[i] = max(r[i][0] - r[i][1], l_limit)
w_left[i] = count_walls(left_bound[i], r[i][0])
r_limit = r[i + 1][0] - 1 if i < n - 1 else float("inf")
right_bound[i] = min(r[i][0] + r[i][1], r_limit)
w_right[i] = count_walls(r[i][0], right_bound[i])
# dp0: robot i fires Left
# dp1: robot i fires Right
dp0 = w_left[0]
dp1 = w_right[0]
for i in range(1, n):
if right_bound[i - 1] >= left_bound[i]:
w_both = count_walls(r[i - 1][0], r[i][0])
else:
w_both = w_right[i - 1] + w_left[i]
next_dp0 = max(dp0 + w_left[i], dp1 - w_right[i - 1] + w_both)
next_dp1 = max(dp0, dp1) + w_right[i]
dp0, dp1 = next_dp0, next_dp1
return max(dp0, dp1)
/**
* Problem: LeetCode 3661 - Maximum Walls Destroyed by Robots
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxWalls(vector<int>& robots, vector<int>& distance, vector<int>& walls) {
int n = robots.size();
vector<pair<int, int>> r(n);
for (int i = 0; i < n; i++) {
r[i] = {robots[i], distance[i]};
}
sort(r.begin(), r.end());
sort(walls.begin(), walls.end());
auto countWalls = [&](long long low, long long high) -> int {
if (low > high) return 0;
auto start = lower_bound(walls.begin(), walls.end(), low);
auto end = upper_bound(walls.begin(), walls.end(), high);
return max(0, (int)(end - start));
};
vector<long long> leftBound(n);
vector<long long> rightBound(n);
vector<int> wLeft(n);
vector<int> wRight(n);
for (int i = 0; i < n; i++) {
long long lLimit = (i > 0) ? (long long)r[i - 1].first + 1 : -2e18;
leftBound[i] = max((long long)r[i].first - r[i].second, lLimit);
wLeft[i] = countWalls(leftBound[i], r[i].first);
long long rLimit = (i < n - 1) ? (long long)r[i + 1].first - 1 : 2e18;
rightBound[i] = min((long long)r[i].first + r[i].second, rLimit);
wRight[i] = countWalls(r[i].first, rightBound[i]);
}
// dp0: robot i fires Left
// dp1: robot i fires Right
int dp0 = wLeft[0];
int dp1 = wRight[0];
for (int i = 1; i < n; i++) {
int wBoth = 0;
if (rightBound[i - 1] >= leftBound[i]) {
wBoth = countWalls(r[i - 1].first, r[i].first);
} else {
wBoth = wRight[i - 1] + wLeft[i];
}
int nextDp0 = max(dp0 + wLeft[i], dp1 - wRight[i - 1] + wBoth);
int nextDp1 = max(dp0, dp1) + wRight[i];
dp0 = nextDp0;
dp1 = nextDp1;
}
return max(dp0, dp1);
}
};
/**
* Problem: LeetCode 3661 - Maximum Walls Destroyed by Robots
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var maxWalls = function(robots, distance, walls) {
const n = robots.length;
const r = [];
for (let i = 0; i < n; i++) {
r.push({ pos: robots[i], dist: distance[i] });
}
r.sort((a, b) => a.pos - b.pos);
walls.sort((a, b) => a - b);
function countWalls(low, high) {
if (low > high) return 0;
let left = 0, right = walls.length;
while (left < right) {
const mid = (left + right) >> 1;
if (walls[mid] >= low) right = mid;
else left = mid + 1;
}
const start = left;
left = 0; right = walls.length;
while (left < right) {
const mid = (left + right) >> 1;
if (walls[mid] > high) right = mid;
else left = mid + 1;
}
const end = left;
return Math.max(0, end - start);
}
const leftBound = new Array(n);
const rightBound = new Array(n);
const wLeft = new Array(n);
const wRight = new Array(n);
for (let i = 0; i < n; i++) {
const lLimit = (i > 0) ? r[i - 1].pos + 1 : -Infinity;
leftBound[i] = Math.max(r[i].pos - r[i].dist, lLimit);
wLeft[i] = countWalls(leftBound[i], r[i].pos);
const rLimit = (i < n - 1) ? r[i + 1].pos - 1 : Infinity;
rightBound[i] = Math.min(r[i].pos + r[i].dist, rLimit);
wRight[i] = countWalls(r[i].pos, rightBound[i]);
}
// dp0: robot i fires Left
// dp1: robot i fires Right
let dp0 = wLeft[0];
let dp1 = wRight[0];
for (let i = 1; i < n; i++) {
let wBoth = 0;
if (rightBound[i - 1] >= leftBound[i]) {
wBoth = countWalls(r[i - 1].pos, r[i].pos);
} else {
wBoth = wRight[i - 1] + wLeft[i];
}
const nextDp0 = Math.max(dp0 + wLeft[i], dp1 - wRight[i - 1] + wBoth);
const nextDp1 = Math.max(dp0, dp1) + wRight[i];
dp0 = nextDp0;
dp1 = nextDp1;
}
return Math.max(dp0, dp1);
};