-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path636.cpp
More file actions
25 lines (25 loc) · 716 Bytes
/
Copy path636.cpp
File metadata and controls
25 lines (25 loc) · 716 Bytes
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
// singleScan-iteration.cpp
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string> &logs) {
vector<int> res(n, 0);
int lastTime = 0, lastId = 0;
vector<int> st(1, 0);
for (auto &str : logs) {
int first = str.find(':'), second = str.rfind(':');
int id = stoi(str.substr(0, first));
bool end = second - first == 4; // sizeof(":end:")-1;
int timestamp = stoi(str.substr(second + 1));
if (end) {
res[id] += timestamp - lastTime + 1;
lastTime = timestamp + 1;
st.pop_back();
} else {
res[st.back()] += timestamp - lastTime;
lastTime = timestamp;
st.push_back(id);
}
}
return res;
}
};