Skip to content

Commit 64784b2

Browse files
authored
feat: add solutions to lc problem: No.2751
1 parent 2bb4569 commit 64784b2

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public:
3+
vector<int> survivedRobotsHealths(vector<int>& positions,
4+
vector<int>& healths, string directions) {
5+
int n = positions.size();
6+
vector<int> indices(n);
7+
8+
iota(indices.begin(), indices.end(), 0);
9+
stack<int> st;
10+
11+
auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };
12+
13+
sort(begin(indices), end(indices), lambda);
14+
15+
vector<int> result;
16+
for (int currentIndex : indices) {
17+
if (directions[currentIndex] == 'R') {
18+
st.push(currentIndex);
19+
} else {
20+
while (!st.empty() && healths[currentIndex] > 0) {
21+
int topIndex = st.top();
22+
st.pop();
23+
24+
if (healths[topIndex] > healths[currentIndex]) {
25+
healths[topIndex] -= 1;
26+
healths[currentIndex] = 0;
27+
st.push(topIndex);
28+
} else if (healths[topIndex] < healths[currentIndex]) {
29+
healths[currentIndex] -= 1;
30+
healths[topIndex] = 0;
31+
} else {
32+
healths[currentIndex] = 0;
33+
healths[topIndex] = 0;
34+
}
35+
}
36+
}
37+
}
38+
39+
for (int i = 0; i < n; ++i) {
40+
if (healths[i] > 0) {
41+
result.push_back(healths[i]);
42+
}
43+
}
44+
return result;
45+
}
46+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
func survivedRobotsHealths(positions []int, healths []int, directions string) []int {
2+
n := len(positions)
3+
indices := make([]int, n)
4+
for i := range indices {
5+
indices[i] = i
6+
}
7+
8+
sort.Slice(indices, func(i, j int) bool {
9+
return positions[indices[i]] < positions[indices[j]]
10+
})
11+
12+
stack := []int{}
13+
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]
21+
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+
}
33+
}
34+
}
35+
}
36+
37+
result := []int{}
38+
for _, health := range healths {
39+
if health > 0 {
40+
result = append(result, health)
41+
}
42+
}
43+
44+
return result
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
3+
int n = positions.length;
4+
Integer[] indices = new Integer[n];
5+
for (int i = 0; i < n; i++) {
6+
indices[i] = i;
7+
}
8+
9+
Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));
10+
11+
Stack<Integer> stack = new Stack<>();
12+
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();
19+
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+
}
31+
}
32+
}
33+
}
34+
35+
List<Integer> result = new ArrayList<>();
36+
for (int health : healths) {
37+
if (health > 0) {
38+
result.add(health);
39+
}
40+
}
41+
42+
return result;
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
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 = []
8+
9+
indices.sort(key=lambda i: positions[i])
10+
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()
17+
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

0 commit comments

Comments
 (0)