Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 296498d

Browse files
Nothing-avilYashwanth137
andauthored
feat: add solutions to lc problem: No.2751 (#3270)
Co-authored-by: Yash <75321458+Yashwanth137@users.noreply.github.com>
1 parent 772a22d commit 296498d

File tree

6 files changed

+492
-6
lines changed

6 files changed

+492
-6
lines changed

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

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,186 @@ tags:
9090
#### Python3
9191

9292
```python
93+
class Solution:
94+
def survivedRobotsHealths(
95+
self, positions: List[int], healths: List[int], directions: str
96+
) -> List[int]:
97+
n = len(positions)
98+
indices = list(range(n))
99+
stack = []
100+
101+
indices.sort(key=lambda i: positions[i])
102+
103+
for currentIndex in indices:
104+
if directions[currentIndex] == "R":
105+
stack.append(currentIndex)
106+
else:
107+
while stack and healths[currentIndex] > 0:
108+
topIndex = stack.pop()
109+
110+
if healths[topIndex] > healths[currentIndex]:
111+
healths[topIndex] -= 1
112+
healths[currentIndex] = 0
113+
stack.append(topIndex)
114+
elif healths[topIndex] < healths[currentIndex]:
115+
healths[currentIndex] -= 1
116+
healths[topIndex] = 0
117+
else:
118+
healths[currentIndex] = 0
119+
healths[topIndex] = 0
120+
121+
result = [health for health in healths if health > 0]
122+
return result
93123

94124
```
95125

96126
#### Java
97127

98128
```java
99-
129+
class Solution {
130+
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
131+
int n = positions.length;
132+
Integer[] indices = new Integer[n];
133+
for (int i = 0; i < n; i++) {
134+
indices[i] = i;
135+
}
136+
137+
Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));
138+
139+
Stack<Integer> stack = new Stack<>();
140+
141+
for (int currentIndex : indices) {
142+
if (directions.charAt(currentIndex) == 'R') {
143+
stack.push(currentIndex);
144+
} else {
145+
while (!stack.isEmpty() && healths[currentIndex] > 0) {
146+
int topIndex = stack.pop();
147+
148+
if (healths[topIndex] > healths[currentIndex]) {
149+
healths[topIndex] -= 1;
150+
healths[currentIndex] = 0;
151+
stack.push(topIndex);
152+
} else if (healths[topIndex] < healths[currentIndex]) {
153+
healths[currentIndex] -= 1;
154+
healths[topIndex] = 0;
155+
} else {
156+
healths[currentIndex] = 0;
157+
healths[topIndex] = 0;
158+
}
159+
}
160+
}
161+
}
162+
163+
List<Integer> result = new ArrayList<>();
164+
for (int health : healths) {
165+
if (health > 0) {
166+
result.add(health);
167+
}
168+
}
169+
170+
return result;
171+
}
172+
}
100173
```
101174

102175
#### C++
103176

104177
```cpp
105-
178+
class Solution {
179+
public:
180+
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
181+
int n = positions.size();
182+
vector<int> indices(n);
183+
184+
iota(indices.begin(), indices.end(), 0);
185+
stack<int> st;
186+
187+
auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };
188+
189+
sort(begin(indices), end(indices), lambda);
190+
191+
vector<int> result;
192+
for (int currentIndex : indices) {
193+
if (directions[currentIndex] == 'R') {
194+
st.push(currentIndex);
195+
} else {
196+
while (!st.empty() && healths[currentIndex] > 0) {
197+
int topIndex = st.top();
198+
st.pop();
199+
200+
if (healths[topIndex] > healths[currentIndex]) {
201+
healths[topIndex] -= 1;
202+
healths[currentIndex] = 0;
203+
st.push(topIndex);
204+
} else if (healths[topIndex] < healths[currentIndex]) {
205+
healths[currentIndex] -= 1;
206+
healths[topIndex] = 0;
207+
} else {
208+
healths[currentIndex] = 0;
209+
healths[topIndex] = 0;
210+
}
211+
}
212+
}
213+
}
214+
215+
for (int i = 0; i < n; ++i) {
216+
if (healths[i] > 0) {
217+
result.push_back(healths[i]);
218+
}
219+
}
220+
return result;
221+
}
222+
};
106223
```
107224

108225
#### Go
109226

110227
```go
111-
228+
func survivedRobotsHealths(positions []int, healths []int, directions string) []int {
229+
n := len(positions)
230+
indices := make([]int, n)
231+
for i := range indices {
232+
indices[i] = i
233+
}
234+
235+
sort.Slice(indices, func(i, j int) bool {
236+
return positions[indices[i]] < positions[indices[j]]
237+
})
238+
239+
stack := []int{}
240+
241+
for _, currentIndex := range indices {
242+
if directions[currentIndex] == 'R' {
243+
stack = append(stack, currentIndex)
244+
} else {
245+
for len(stack) > 0 && healths[currentIndex] > 0 {
246+
topIndex := stack[len(stack)-1]
247+
stack = stack[:len(stack)-1]
248+
249+
if healths[topIndex] > healths[currentIndex] {
250+
healths[topIndex] -= 1
251+
healths[currentIndex] = 0
252+
stack = append(stack, topIndex)
253+
} else if healths[topIndex] < healths[currentIndex] {
254+
healths[currentIndex] -= 1
255+
healths[topIndex] = 0
256+
} else {
257+
healths[currentIndex] = 0
258+
healths[topIndex] = 0
259+
}
260+
}
261+
}
262+
}
263+
264+
result := []int{}
265+
for _, health := range healths {
266+
if health > 0 {
267+
result = append(result, health)
268+
}
269+
}
270+
271+
return result
272+
}
112273
```
113274

114275
<!-- tabs:end -->

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

Lines changed: 164 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,186 @@ tags:
9090
#### Python3
9191

9292
```python
93+
class Solution:
94+
def survivedRobotsHealths(
95+
self, positions: List[int], healths: List[int], directions: str
96+
) -> List[int]:
97+
n = len(positions)
98+
indices = list(range(n))
99+
stack = []
100+
101+
indices.sort(key=lambda i: positions[i])
102+
103+
for currentIndex in indices:
104+
if directions[currentIndex] == "R":
105+
stack.append(currentIndex)
106+
else:
107+
while stack and healths[currentIndex] > 0:
108+
topIndex = stack.pop()
109+
110+
if healths[topIndex] > healths[currentIndex]:
111+
healths[topIndex] -= 1
112+
healths[currentIndex] = 0
113+
stack.append(topIndex)
114+
elif healths[topIndex] < healths[currentIndex]:
115+
healths[currentIndex] -= 1
116+
healths[topIndex] = 0
117+
else:
118+
healths[currentIndex] = 0
119+
healths[topIndex] = 0
120+
121+
result = [health for health in healths if health > 0]
122+
return result
93123

94124
```
95125

96126
#### Java
97127

98128
```java
99-
129+
class Solution {
130+
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
131+
int n = positions.length;
132+
Integer[] indices = new Integer[n];
133+
for (int i = 0; i < n; i++) {
134+
indices[i] = i;
135+
}
136+
137+
Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));
138+
139+
Stack<Integer> stack = new Stack<>();
140+
141+
for (int currentIndex : indices) {
142+
if (directions.charAt(currentIndex) == 'R') {
143+
stack.push(currentIndex);
144+
} else {
145+
while (!stack.isEmpty() && healths[currentIndex] > 0) {
146+
int topIndex = stack.pop();
147+
148+
if (healths[topIndex] > healths[currentIndex]) {
149+
healths[topIndex] -= 1;
150+
healths[currentIndex] = 0;
151+
stack.push(topIndex);
152+
} else if (healths[topIndex] < healths[currentIndex]) {
153+
healths[currentIndex] -= 1;
154+
healths[topIndex] = 0;
155+
} else {
156+
healths[currentIndex] = 0;
157+
healths[topIndex] = 0;
158+
}
159+
}
160+
}
161+
}
162+
163+
List<Integer> result = new ArrayList<>();
164+
for (int health : healths) {
165+
if (health > 0) {
166+
result.add(health);
167+
}
168+
}
169+
170+
return result;
171+
}
172+
}
100173
```
101174

102175
#### C++
103176

104177
```cpp
105-
178+
class Solution {
179+
public:
180+
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
181+
int n = positions.size();
182+
vector<int> indices(n);
183+
184+
iota(indices.begin(), indices.end(), 0);
185+
stack<int> st;
186+
187+
auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };
188+
189+
sort(begin(indices), end(indices), lambda);
190+
191+
vector<int> result;
192+
for (int currentIndex : indices) {
193+
if (directions[currentIndex] == 'R') {
194+
st.push(currentIndex);
195+
} else {
196+
while (!st.empty() && healths[currentIndex] > 0) {
197+
int topIndex = st.top();
198+
st.pop();
199+
200+
if (healths[topIndex] > healths[currentIndex]) {
201+
healths[topIndex] -= 1;
202+
healths[currentIndex] = 0;
203+
st.push(topIndex);
204+
} else if (healths[topIndex] < healths[currentIndex]) {
205+
healths[currentIndex] -= 1;
206+
healths[topIndex] = 0;
207+
} else {
208+
healths[currentIndex] = 0;
209+
healths[topIndex] = 0;
210+
}
211+
}
212+
}
213+
}
214+
215+
for (int i = 0; i < n; ++i) {
216+
if (healths[i] > 0) {
217+
result.push_back(healths[i]);
218+
}
219+
}
220+
return result;
221+
}
222+
};
106223
```
107224

108225
#### Go
109226

110227
```go
111-
228+
func survivedRobotsHealths(positions []int, healths []int, directions string) []int {
229+
n := len(positions)
230+
indices := make([]int, n)
231+
for i := range indices {
232+
indices[i] = i
233+
}
234+
235+
sort.Slice(indices, func(i, j int) bool {
236+
return positions[indices[i]] < positions[indices[j]]
237+
})
238+
239+
stack := []int{}
240+
241+
for _, currentIndex := range indices {
242+
if directions[currentIndex] == 'R' {
243+
stack = append(stack, currentIndex)
244+
} else {
245+
for len(stack) > 0 && healths[currentIndex] > 0 {
246+
topIndex := stack[len(stack)-1]
247+
stack = stack[:len(stack)-1]
248+
249+
if healths[topIndex] > healths[currentIndex] {
250+
healths[topIndex] -= 1
251+
healths[currentIndex] = 0
252+
stack = append(stack, topIndex)
253+
} else if healths[topIndex] < healths[currentIndex] {
254+
healths[currentIndex] -= 1
255+
healths[topIndex] = 0
256+
} else {
257+
healths[currentIndex] = 0
258+
healths[topIndex] = 0
259+
}
260+
}
261+
}
262+
}
263+
264+
result := []int{}
265+
for _, health := range healths {
266+
if health > 0 {
267+
result = append(result, health)
268+
}
269+
}
270+
271+
return result
272+
}
112273
```
113274

114275
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /