// TODO: need improve!!!
class Log {
public:
int id;
bool start;
int timestamp;
int comp; // compasation
Log() {
id = 0;
timestamp = 0;
comp = 0;
start = false;
}
};
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string>& logs) {
vector<int> res(n, 0);
stack<Log> s;
for (auto &log : logs) {
auto item = parseLog(log);
if (item.start)
s.push(item);
else {
int comp = item.timestamp - s.top().timestamp + 1;
int exTime = item.timestamp - s.top().timestamp + 1 - s.top().comp;
res[item.id] += exTime;
s.pop();
if (!s.empty()) {
s.top().comp += comp;
}
}
}
return res;
}
Log parseLog(const string& s) {
int col1 = s.find(":");
int col2 = s.find(":", col1 + 1);
int id = stoi(s.substr(0, col1));
int time = stoi(s.substr(col2 + 1));
string state = s.substr(col1 + 1, col2 - col1 - 1);
Log log;
if (state == "start")
log.start = true;
log.id = id;
log.timestamp = time;
return log;
}
};