-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathRobot Collisions.cpp
More file actions
113 lines (93 loc) · 3.68 KB
/
Robot Collisions.cpp
File metadata and controls
113 lines (93 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Scroll below to see JAVA Code also */
/*
MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=fkcA9zvP7_w
Company Tags : Will update soon
Leetcode Link : https://leetcode.com/problems/robot-collisions/description
*/
/*********************************************************************** C++ ********************************************************************************/
//Using Stack
//T.C : O(nlogn)
//T.C : O(n)
class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
int n = positions.size();
vector<int> indices(n);
iota(indices.begin(), indices.end(), 0); //This will fill the array as -> 0, 1, 2, 3, 4, n-1
stack<int> st;
auto lambda = [&](int i, int j) {
return positions[i] < positions[j];
};
sort(begin(indices), end(indices), lambda);
vector<int> result;
for (int currentIndex : indices) {
if (directions[currentIndex] == 'R') {
st.push(currentIndex);
} else {
while (!st.empty() && healths[currentIndex] > 0) {
int topIndex = st.top();
st.pop();
if (healths[topIndex] > healths[currentIndex]) {
healths[topIndex] -= 1;
healths[currentIndex] = 0;
st.push(topIndex);
} else if (healths[topIndex] < healths[currentIndex]) {
healths[currentIndex] -= 1;
healths[topIndex] = 0;
} else {
healths[currentIndex] = 0;
healths[topIndex] = 0;
}
}
}
}
for (int i = 0; i < n; ++i) {
if (healths[i] > 0) {
result.push_back(healths[i]);
}
}
return result;
}
};
/*********************************************************************** JAVA ********************************************************************************/
//Using Stack
//T.C : O(nlogn)
//T.C : O(n)
class Solution {
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
int n = positions.length;
Integer[] indices = new Integer[n];
for (int i = 0; i < n; i++) {
indices[i] = i;
}
Stack<Integer> stack = new Stack<>();
Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));
List<Integer> result = new ArrayList<>();
for (int currentIndex : indices) {
if (directions.charAt(currentIndex) == 'R') {
stack.push(currentIndex);
} else {
while (!stack.isEmpty() && healths[currentIndex] > 0) {
int topIndex = stack.pop();
if (healths[topIndex] > healths[currentIndex]) {
healths[topIndex] -= 1;
healths[currentIndex] = 0;
stack.push(topIndex);
} else if (healths[topIndex] < healths[currentIndex]) {
healths[currentIndex] -= 1;
healths[topIndex] = 0;
} else {
healths[currentIndex] = 0;
healths[topIndex] = 0;
}
}
}
}
for (int i = 0; i < n; i++) {
if (healths[i] > 0) {
result.add(healths[i]);
}
}
return result;
}
}