Skip to content

Commit 3937339

Browse files
authored
feat: add solutions for lc No.2751 (#5112)
1 parent b13ed85 commit 3937339

File tree

8 files changed

+477
-485
lines changed

8 files changed

+477
-485
lines changed

solution/2700-2799/2751.Robot Collisions/README.md

Lines changed: 165 additions & 164 deletions
Large diffs are not rendered by default.

solution/2700-2799/2751.Robot Collisions/README_EN.md

Lines changed: 165 additions & 164 deletions
Large diffs are not rendered by default.

solution/2700-2799/2751.Robot Collisions/Solution.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@ class Solution {
22
public:
33
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
44
int n = positions.size();
5-
vector<int> indices(n);
5+
vector<int> idx(n);
6+
iota(idx.begin(), idx.end(), 0);
67

7-
iota(indices.begin(), indices.end(), 0);
8-
stack<int> st;
8+
sort(idx.begin(), idx.end(), [&](int a, int b) {
9+
return positions[a] < positions[b];
10+
});
911

10-
auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };
12+
vector<int> stk;
1113

12-
sort(begin(indices), end(indices), lambda);
14+
for (int i : idx) {
15+
if (directions[i] == 'R') {
16+
stk.push_back(i);
17+
continue;
18+
}
1319

14-
vector<int> result;
15-
for (int currentIndex : indices) {
16-
if (directions[currentIndex] == 'R') {
17-
st.push(currentIndex);
18-
} else {
19-
while (!st.empty() && healths[currentIndex] > 0) {
20-
int topIndex = st.top();
21-
st.pop();
20+
while (!stk.empty() && healths[i] > 0) {
21+
int j = stk.back();
2222

23-
if (healths[topIndex] > healths[currentIndex]) {
24-
healths[topIndex] -= 1;
25-
healths[currentIndex] = 0;
26-
st.push(topIndex);
27-
} else if (healths[topIndex] < healths[currentIndex]) {
28-
healths[currentIndex] -= 1;
29-
healths[topIndex] = 0;
30-
} else {
31-
healths[currentIndex] = 0;
32-
healths[topIndex] = 0;
33-
}
23+
if (healths[j] > healths[i]) {
24+
healths[j]--;
25+
healths[i] = 0;
26+
} else if (healths[j] < healths[i]) {
27+
healths[i]--;
28+
healths[j] = 0;
29+
stk.pop_back();
30+
} else {
31+
healths[i] = healths[j] = 0;
32+
stk.pop_back();
33+
break;
3434
}
3535
}
3636
}
3737

38-
for (int i = 0; i < n; ++i) {
39-
if (healths[i] > 0) {
40-
result.push_back(healths[i]);
38+
vector<int> ans;
39+
for (int h : healths) {
40+
if (h > 0) {
41+
ans.push_back(h);
4142
}
4243
}
43-
return result;
44+
return ans;
4445
}
4546
};
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
func survivedRobotsHealths(positions []int, healths []int, directions string) []int {
22
n := len(positions)
3-
indices := make([]int, n)
4-
for i := range indices {
5-
indices[i] = i
3+
idx := make([]int, n)
4+
for i := range idx {
5+
idx[i] = i
66
}
77

8-
sort.Slice(indices, func(i, j int) bool {
9-
return positions[indices[i]] < positions[indices[j]]
8+
sort.Slice(idx, func(i, j int) bool {
9+
return positions[idx[i]] < positions[idx[j]]
1010
})
1111

12-
stack := []int{}
12+
stk := []int{}
1313

14-
for _, currentIndex := range indices {
15-
if directions[currentIndex] == 'R' {
16-
stack = append(stack, currentIndex)
17-
} else {
18-
for len(stack) > 0 && healths[currentIndex] > 0 {
19-
topIndex := stack[len(stack)-1]
20-
stack = stack[:len(stack)-1]
14+
for _, i := range idx {
15+
if directions[i] == 'R' {
16+
stk = append(stk, i)
17+
continue
18+
}
19+
20+
for len(stk) > 0 && healths[i] > 0 {
21+
j := stk[len(stk)-1]
2122

22-
if healths[topIndex] > healths[currentIndex] {
23-
healths[topIndex] -= 1
24-
healths[currentIndex] = 0
25-
stack = append(stack, topIndex)
26-
} else if healths[topIndex] < healths[currentIndex] {
27-
healths[currentIndex] -= 1
28-
healths[topIndex] = 0
29-
} else {
30-
healths[currentIndex] = 0
31-
healths[topIndex] = 0
32-
}
23+
if healths[j] > healths[i] {
24+
healths[j]--
25+
healths[i] = 0
26+
} else if healths[j] < healths[i] {
27+
healths[i]--
28+
healths[j] = 0
29+
stk = stk[:len(stk)-1]
30+
} else {
31+
healths[i], healths[j] = 0, 0
32+
stk = stk[:len(stk)-1]
33+
break
3334
}
3435
}
3536
}
3637

37-
result := []int{}
38-
for _, health := range healths {
39-
if health > 0 {
40-
result = append(result, health)
38+
ans := []int{}
39+
for _, h := range healths {
40+
if h > 0 {
41+
ans = append(ans, h)
4142
}
4243
}
43-
44-
return result
45-
}
44+
return ans
45+
}
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
class Solution {
22
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
33
int n = positions.length;
4-
Integer[] indices = new Integer[n];
5-
for (int i = 0; i < n; i++) {
6-
indices[i] = i;
7-
}
4+
Integer[] idx = new Integer[n];
5+
Arrays.setAll(idx, i -> i);
6+
Arrays.sort(idx, (a, b) -> positions[a] - positions[b]);
87

9-
Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));
8+
Deque<Integer> stk = new ArrayDeque<>();
109

11-
Stack<Integer> stack = new Stack<>();
10+
for (int i : idx) {
11+
if (directions.charAt(i) == 'R') {
12+
stk.push(i);
13+
continue;
14+
}
1215

13-
for (int currentIndex : indices) {
14-
if (directions.charAt(currentIndex) == 'R') {
15-
stack.push(currentIndex);
16-
} else {
17-
while (!stack.isEmpty() && healths[currentIndex] > 0) {
18-
int topIndex = stack.pop();
16+
while (!stk.isEmpty() && healths[i] > 0) {
17+
int j = stk.peek();
1918

20-
if (healths[topIndex] > healths[currentIndex]) {
21-
healths[topIndex] -= 1;
22-
healths[currentIndex] = 0;
23-
stack.push(topIndex);
24-
} else if (healths[topIndex] < healths[currentIndex]) {
25-
healths[currentIndex] -= 1;
26-
healths[topIndex] = 0;
27-
} else {
28-
healths[currentIndex] = 0;
29-
healths[topIndex] = 0;
30-
}
19+
if (healths[j] > healths[i]) {
20+
healths[j]--;
21+
healths[i] = 0;
22+
} else if (healths[j] < healths[i]) {
23+
healths[i]--;
24+
healths[j] = 0;
25+
stk.pop();
26+
} else {
27+
healths[i] = healths[j] = 0;
28+
stk.pop();
29+
break;
3130
}
3231
}
3332
}
3433

35-
List<Integer> result = new ArrayList<>();
36-
for (int health : healths) {
37-
if (health > 0) {
38-
result.add(health);
34+
List<Integer> ans = new ArrayList<>();
35+
for (int h : healths) {
36+
if (h > 0) {
37+
ans.add(h);
3938
}
4039
}
41-
42-
return result;
40+
return ans;
4341
}
44-
}
42+
}

solution/2700-2799/2751.Robot Collisions/Solution.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,34 @@
55
* @return {number[]}
66
*/
77
var survivedRobotsHealths = function (positions, healths, directions) {
8-
const idx = Array.from({ length: positions.length }, (_, i) => i);
9-
const stk = [];
8+
const n = positions.length;
9+
const idx = Array.from({ length: n }, (_, i) => i).sort((a, b) => positions[a] - positions[b]);
1010

11-
idx.sort((a, b) => positions[a] - positions[b]);
11+
const stk = [];
1212

13-
for (let iRight of idx) {
14-
while (stk.length) {
15-
const iLeft = stk.at(-1);
16-
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
17-
if (!havePair) break;
13+
for (const i of idx) {
14+
if (directions[i] === 'R') {
15+
stk.push(i);
16+
continue;
17+
}
1818

19-
if (healths[iLeft] === healths[iRight]) {
20-
healths[iLeft] = healths[iRight] = iRight = -1;
21-
stk.pop();
22-
break;
23-
}
19+
while (stk.length && healths[i] > 0) {
20+
const j = stk[stk.length - 1];
2421

25-
if (healths[iLeft] < healths[iRight]) {
26-
healths[iLeft] = -1;
27-
healths[iRight]--;
22+
if (healths[j] > healths[i]) {
23+
healths[j]--;
24+
healths[i] = 0;
25+
} else if (healths[j] < healths[i]) {
26+
healths[i]--;
27+
healths[j] = 0;
2828
stk.pop();
2929
} else {
30-
healths[iRight] = iRight = -1;
31-
healths[iLeft]--;
30+
healths[i] = healths[j] = 0;
31+
stk.pop();
3232
break;
3333
}
3434
}
35-
36-
if (iRight !== -1) stk.push(iRight);
3735
}
3836

39-
return healths.filter(i => ~i);
37+
return healths.filter(h => h > 0);
4038
};
Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
class Solution:
2-
def survivedRobotsHealths(
3-
self, positions: List[int], healths: List[int], directions: str
4-
) -> List[int]:
5-
n = len(positions)
6-
indices = list(range(n))
7-
stack = []
2+
def survivedRobotsHealths(self, positions, healths, directions):
3+
idx = sorted(range(len(positions)), key=lambda i: positions[i])
4+
stk = []
85

9-
indices.sort(key=lambda i: positions[i])
6+
for i in idx:
7+
if directions[i] == "R":
8+
stk.append(i)
9+
continue
1010

11-
for currentIndex in indices:
12-
if directions[currentIndex] == "R":
13-
stack.append(currentIndex)
14-
else:
15-
while stack and healths[currentIndex] > 0:
16-
topIndex = stack.pop()
11+
while stk and healths[i]:
12+
j = stk[-1]
13+
if healths[j] > healths[i]:
14+
healths[j] -= 1
15+
healths[i] = 0
16+
elif healths[j] < healths[i]:
17+
healths[i] -= 1
18+
healths[j] = 0
19+
stk.pop()
20+
else:
21+
healths[i] = healths[j] = 0
22+
stk.pop()
23+
break
1724

18-
if healths[topIndex] > healths[currentIndex]:
19-
healths[topIndex] -= 1
20-
healths[currentIndex] = 0
21-
stack.append(topIndex)
22-
elif healths[topIndex] < healths[currentIndex]:
23-
healths[currentIndex] -= 1
24-
healths[topIndex] = 0
25-
else:
26-
healths[currentIndex] = 0
27-
healths[topIndex] = 0
28-
29-
result = [health for health in healths if health > 0]
30-
return result
25+
return [h for h in healths if h > 0]

solution/2700-2799/2751.Robot Collisions/Solution.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,34 @@ function survivedRobotsHealths(
33
healths: number[],
44
directions: string,
55
): number[] {
6-
const idx = Array.from({ length: positions.length }, (_, i) => i);
7-
const stk: number[] = [];
6+
const n = positions.length;
7+
const idx = Array.from({ length: n }, (_, i) => i).sort((a, b) => positions[a] - positions[b]);
88

9-
idx.sort((a, b) => positions[a] - positions[b]);
9+
const stk: number[] = [];
1010

11-
for (let iRight of idx) {
12-
while (stk.length) {
13-
const iLeft = stk.at(-1)!;
14-
const havePair = directions[iLeft] === 'R' && directions[iRight] === 'L';
15-
if (!havePair) break;
11+
for (const i of idx) {
12+
if (directions[i] === 'R') {
13+
stk.push(i);
14+
continue;
15+
}
1616

17-
if (healths[iLeft] === healths[iRight]) {
18-
healths[iLeft] = healths[iRight] = iRight = -1;
19-
stk.pop();
20-
break;
21-
}
17+
while (stk.length && healths[i] > 0) {
18+
const j = stk[stk.length - 1];
2219

23-
if (healths[iLeft] < healths[iRight]) {
24-
healths[iLeft] = -1;
25-
healths[iRight]--;
20+
if (healths[j] > healths[i]) {
21+
healths[j]--;
22+
healths[i] = 0;
23+
} else if (healths[j] < healths[i]) {
24+
healths[i]--;
25+
healths[j] = 0;
2626
stk.pop();
2727
} else {
28-
healths[iRight] = iRight = -1;
29-
healths[iLeft]--;
28+
healths[i] = healths[j] = 0;
29+
stk.pop();
3030
break;
3131
}
3232
}
33-
34-
if (iRight !== -1) stk.push(iRight);
3533
}
3634

37-
return healths.filter(i => ~i);
35+
return healths.filter(h => h > 0);
3836
}

0 commit comments

Comments
 (0)