Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Solution {
- public:
- vector<int> exclusiveTime(int n, vector<string>& logs) {
- stack<int> s;
- vector<int> ans(n, 0);
- for(int i=0; i<logs.size(); i++){
- int id, time, lastTime;
- string type;
- string str = logs[i];
- size_t pos = str.find(':');
- id = stoi(str.substr(0, pos));
- str.erase(0, pos+1);
- pos = str.find(':');
- type = str.substr(0, pos);
- str.erase(0, pos+1);
- time = stoi(str);
- if(type == "start"){
- if(!s.empty()){
- ans[s.top()] += (time - lastTime - 1);
- }
- s.push(id);
- }
- else{
- ans[s.top()] += (time - lastTime + 1);
- s.pop();
- }
- lastTime = time;
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment