-
Notifications
You must be signed in to change notification settings - Fork 3
/
636.cpp
39 lines (39 loc) · 1.15 KB
/
636.cpp
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
class Solution {
public:
vector<int> parser(string& log) {
stringstream ss(log);
string token;
vector<string> information;
while(getline(ss, token, ':')) {
information.push_back(token);
}
int func = stoi(information[0]);
int isStart = 1;
if (information[1][0] == 'e') isStart = 0;
int time = stoi(information[2]);
return {func, isStart, time};
}
vector<int> exclusiveTime(int n, vector<string>& logs) {
stack<int> st;
int currentTime = 0;
vector<int> res(n, 0);
for (auto& log : logs) {
vector<int> info = parser(log);
if (info[1]) {
if (!st.empty()) {
int prevFunc = st.top();
res[prevFunc] += info[2] - currentTime;
}
currentTime = info[2];
st.push(info[0]);
}
else {
int currFunc = st.top();
st.pop();
res[currFunc] += info[2] - currentTime + 1;
currentTime = info[2] + 1;
}
}
return res;
}
};