Untitled

 avatar
unknown
plain_text
a year ago
889 B
6
Indexable
class Solution {
public:
    vector<int> exclusiveTime(int n, vector<string>& logs) {
        stack<pair<int,int>>cur;
        vector<int>ans(n,0);
        int prevTime=0;

        for(auto &s:logs){
            int num=stoi(s.substr(0,s.find(':')));
            int time=stoi(s.substr(s.rfind(':')+1));
            if(s.find('e')!=-1){//ending encountered case
                ans[num]+=time-prevTime+1;
                cur.pop();
                prevTime=time+1;//once function ends,prevtime goes to the next time
            }else{ //starting case
                if(!cur.empty()) ans[cur.top().first]+=time-prevTime; //accounts for time the old function was running before it got interrupted
                cur.push({num,time});
                prevTime=time; //once function starts,prevtime is the start time
            }
        }
        return ans;
    }
};
Editor is loading...
Leave a Comment