leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0636.cpp (640B)
0 class Solution { 1 public: 2 vector<int> exclusiveTime(int n, const vector<string> &logs) { 3 stack<pair<int, int>> st; 4 vector<int> res(n, 0); 5 int time, id; 6 char op[6]; 7 8 for (const string &log : logs) { 9 sscanf(log.c_str(), "%d:%[^:]:%d", &id, op, &time); 10 if (!strcmp(op, "start")) 11 st.push({id, time}); 12 else { 13 const int added = time - st.top().second + 1; 14 res[id] += added; 15 st.pop(); 16 17 if (!st.empty()) res[st.top().first] -= added; 18 } 19 } 20 return res; 21 } 22 };