Skip to content

Commit 1d5f8f9

Browse files
chore: add LeetCode daily solution
1 parent 298e7be commit 1d5f8f9

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Maximum Walls Destroyed by Robots (Hard)
2+
3+
**Problem ID:** 3661
4+
**Date:** 2026-04-03
5+
**Link:** https://leetcode.com/problems/maximum-walls-destroyed-by-robots/
6+
7+
## Approach
8+
9+
To solve the problem of determining the maximum number of unique walls that can be destroyed by robots, we can adopt a systematic approach that involves sorting and a two-pointer technique.
10+
11+
### Main Idea:
12+
The core idea is to simulate the firing of bullets from each robot in both left and right directions, while keeping track of the walls that can be destroyed. We need to ensure that we account for the distance each robot can shoot and the presence of other robots that may block the bullets.
13+
14+
### Steps:
15+
1. **Data Preparation**:
16+
- Combine the `robots` and `distance` arrays into a list of tuples, where each tuple contains the robot's position and its maximum firing distance.
17+
- Sort this list based on robot positions to facilitate the simulation of bullet firing.
18+
19+
2. **Simulate Bullet Firing**:
20+
- For each robot, calculate the effective range of its bullet in both directions:
21+
- **Left**: From `robot_position - distance` to `robot_position`.
22+
- **Right**: From `robot_position` to `robot_position + distance`.
23+
- Use a set to keep track of unique walls that can be destroyed.
24+
25+
3. **Check for Walls**:
26+
- For the left firing direction, iterate from the leftmost wall within the effective range up to the robot's position. If a wall is found, add it to the set of destroyed walls unless blocked by another robot.
27+
- For the right firing direction, similarly check from the robot's position to the rightmost wall within the effective range.
28+
29+
4. **Handle Blockages**:
30+
- While checking for walls, if a robot is encountered within the effective range (either left or right), stop the firing simulation in that direction to prevent further wall destruction.
31+
32+
5. **Count Unique Walls**:
33+
- After processing all robots, the size of the set containing destroyed walls will give the maximum number of unique walls destroyed.
34+
35+
### Data Structures:
36+
- A list of tuples for robots and their distances.
37+
- A set to store unique wall positions that have been destroyed.
38+
39+
### Complexity:
40+
- Sorting the robots takes \(O(n \log n)\), where \(n\) is the number of robots.
41+
- The wall checking process involves iterating through the walls, which can be done in \(O(m)\), where \(m\) is the number of walls.
42+
- Thus, the overall time complexity is \(O(n \log n + m)\), which is efficient given the constraints.
43+
44+
This approach ensures that we efficiently track the walls that can be destroyed while considering the limitations imposed by the positions of the robots.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int maximumWallsDestroyed(int[] robots, int[] distance, int[] walls) {
5+
Set<Integer> destroyedWalls = new HashSet<>();
6+
Map<Integer, Integer> robotMap = new HashMap<>();
7+
8+
for (int i = 0; i < robots.length; i++) {
9+
robotMap.put(robots[i], distance[i]);
10+
}
11+
12+
Arrays.sort(walls);
13+
Arrays.sort(robots);
14+
15+
for (int robot : robots) {
16+
int maxDistance = robotMap.get(robot);
17+
int leftLimit = Math.max(0, robot - maxDistance);
18+
int rightLimit = robot + maxDistance;
19+
20+
// Destroy walls to the left
21+
for (int wall : walls) {
22+
if (wall < robot && wall >= leftLimit) {
23+
destroyedWalls.add(wall);
24+
}
25+
if (wall >= robot) {
26+
break;
27+
}
28+
}
29+
30+
// Destroy walls to the right
31+
for (int wall : walls) {
32+
if (wall > robot && wall <= rightLimit) {
33+
destroyedWalls.add(wall);
34+
}
35+
if (wall > rightLimit) {
36+
break;
37+
}
38+
}
39+
}
40+
41+
return destroyedWalls.size();
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var maximumWallsDestroyed = function(robots, distance, walls) {
2+
const wallSet = new Set(walls);
3+
const destroyedWalls = new Set();
4+
5+
const robotData = robots.map((pos, idx) => ({
6+
pos,
7+
leftLimit: pos - distance[idx],
8+
rightLimit: pos + distance[idx]
9+
}));
10+
11+
robotData.sort((a, b) => a.pos - b.pos);
12+
13+
for (const { pos, leftLimit, rightLimit } of robotData) {
14+
// Check left direction
15+
for (let wall = leftLimit; wall <= pos; wall++) {
16+
if (wallSet.has(wall)) {
17+
destroyedWalls.add(wall);
18+
}
19+
}
20+
21+
// Check right direction
22+
for (let wall = pos + 1; wall <= rightLimit; wall++) {
23+
if (wallSet.has(wall)) {
24+
destroyedWalls.add(wall);
25+
}
26+
}
27+
}
28+
29+
return destroyedWalls.size;
30+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def maximumRobots(self, robots: List[int], distance: List[int], walls: List[int]) -> int:
3+
wall_set = set(walls)
4+
max_walls_destroyed = 0
5+
6+
# Create a list of robot positions with their effective ranges
7+
robot_ranges = sorted((robots[i], robots[i] - distance[i], robots[i] + distance[i]) for i in range(len(robots)))
8+
9+
# Set to track destroyed walls
10+
destroyed_walls = set()
11+
12+
for pos, left_limit, right_limit in robot_ranges:
13+
# Check left direction
14+
for wall in range(left_limit, pos + 1):
15+
if wall in wall_set:
16+
destroyed_walls.add(wall)
17+
# Check right direction
18+
for wall in range(pos, right_limit + 1):
19+
if wall in wall_set:
20+
destroyed_walls.add(wall)
21+
22+
return len(destroyed_walls)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
412412
- 2026-03-31 — [Lexicographically Smallest Generated String](https://leetcode.com/problems/lexicographically-smallest-generated-string/) (Hard) → `Hard/2026-03-31-3474-Lexicographically-Smallest-Generated-String`
413413
- 2026-04-01 — [Robot Collisions](https://leetcode.com/problems/robot-collisions/) (Hard) → `Hard/2026-04-01-2751-Robot-Collisions`
414414
- 2026-04-02 — [Maximum Amount of Money Robot Can Earn](https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/) (Medium) → `Medium/2026-04-02-3418-Maximum-Amount-of-Money-Robot-Can-Earn`
415+
- 2026-04-03 — [Maximum Walls Destroyed by Robots](https://leetcode.com/problems/maximum-walls-destroyed-by-robots/) (Hard) → `Hard/2026-04-03-3661-Maximum-Walls-Destroyed-by-Robots`

0 commit comments

Comments
 (0)