Skip to content

Commit b3bca37

Browse files
committed
feat: solve No.2751
1 parent 26a1071 commit b3bca37

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

2701-2800/2751. Robot Collisions.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# 2751. Robot Collisions
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Stack, Sorting, Simulation.
5+
- Similar Questions: Asteroid Collision.
6+
7+
## Problem
8+
9+
There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction.
10+
11+
You are given **0-indexed** integer arrays `positions`, `healths`, and a string `directions` (`directions[i]` is either **'L'** for **left** or **'R'** for **right**). All integers in `positions` are **unique**.
12+
13+
All robots start moving on the line** simultaneously** at the **same speed **in their given directions. If two robots ever share the same position while moving, they will **collide**.
14+
15+
If two robots collide, the robot with **lower health** is **removed** from the line, and the health of the other robot **decreases** **by one**. The surviving robot continues in the **same** direction it was going. If both robots have the **same** health, they are both** **removed from the line.
16+
17+
Your task is to determine the **health** of the robots that survive the collisions, in the same **order **that the robots were given,** **i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.
18+
19+
Return **an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.**
20+
21+
**Note:** The positions may be unsorted.
22+
23+
 
24+
25+
 
26+
Example 1:
27+
28+
29+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png)
30+
31+
32+
```
33+
Input: positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
34+
Output: [2,17,9,15,10]
35+
Explanation: No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10].
36+
```
37+
38+
Example 2:
39+
40+
41+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png)
42+
43+
44+
```
45+
Input: positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
46+
Output: [14]
47+
Explanation: There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].
48+
```
49+
50+
Example 3:
51+
52+
53+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png)
54+
55+
56+
```
57+
Input: positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
58+
Output: []
59+
Explanation: Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].
60+
```
61+
62+
 
63+
**Constraints:**
64+
65+
66+
67+
- `1 <= positions.length == healths.length == directions.length == n <= 105`
68+
69+
- `1 <= positions[i], healths[i] <= 109`
70+
71+
- `directions[i] == 'L'` or `directions[i] == 'R'`
72+
73+
- All values in `positions` are distinct
74+
75+
76+
77+
## Solution
78+
79+
```javascript
80+
/**
81+
* @param {number[]} positions
82+
* @param {number[]} healths
83+
* @param {string} directions
84+
* @return {number[]}
85+
*/
86+
var survivedRobotsHealths = function(positions, healths, directions) {
87+
var res = [];
88+
var stack = [];
89+
var robots = positions.map((p, i) => [p, healths[i], directions[i], i]).sort((a, b) => a[0] - b[0]);
90+
for (var i = 0; i < robots.length; i++) {
91+
var robot = robots[i];
92+
if (robot[2] === 'R') {
93+
stack.push(robot);
94+
continue;
95+
}
96+
while (robot[1] > 0 && stack.length) {
97+
if (stack[stack.length - 1][1] < robot[1]) {
98+
stack.pop();
99+
robot[1] -= 1;
100+
} else if (stack[stack.length - 1][1] === robot[1]) {
101+
stack.pop();
102+
robot[1] = 0;
103+
} else {
104+
stack[stack.length - 1][1] -= 1;
105+
robot[1] = 0;
106+
}
107+
}
108+
if (robot[1] > 0) {
109+
res.push(robot);
110+
}
111+
}
112+
res.push(...stack);
113+
return res.sort((a, b) => a[3] - b[3]).map(a => a[1]);
114+
};
115+
```
116+
117+
**Explain:**
118+
119+
nope.
120+
121+
**Complexity:**
122+
123+
* Time complexity : O(n log(n)).
124+
* Space complexity : O(n).

0 commit comments

Comments
 (0)