-
Notifications
You must be signed in to change notification settings - Fork 0
/
636.hpp
60 lines (52 loc) · 1.26 KB
/
636.hpp
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
#ifndef LEETCODE_636_HPP
#define LEETCODE_636_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
#include <sstream>
using namespace std;
class Line {
public:
int fid;
bool start;
int time;
Line(string s) {
auto col1 = s.find(":", 0);
auto col2 = s.find(":", col1 + 1);
fid = stoi(s.substr(0, col1));
start = s.substr(col1 + 1, col2 - (col1 + 1)) == "start";
time = stoi(s.substr(col2 + 1));
}
};
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string> &logs) {
vector<int> times(n, 0);
stack<int> ids;
int time = 0;
for (auto log: logs) {
Line line(log);
if (line.start) {
if (!ids.empty()) {
times[ids.top()] += line.time - time;
}
ids.push(line.fid);
time = line.time;
} else {
line.time++;
times[ids.top()] += line.time - time;
ids.pop();
time = line.time;
}
}
return times;
}
};
#endif //LEETCODE_636_HPP