C++ straight-forward solution beats 100% now


  • 0
    M
        vector<int> exclusiveTime(int n, vector<string>& logs) {
            vector<int> time(n, 0);
            stack<vector<int>> s;
            for(int i=0;i<logs.size();i++) {
                vector<int> t=parse(logs[i]);
                if(t[1]==0) {
                    if(!s.empty()) time[s.top()[0]]+=t[2]-s.top()[1];
                    s.push({t[0], t[2]});
                }
                else {
                    time[t[0]]+=t[2]-s.top()[1]+1;
                    s.pop();
                    if(!s.empty()) s.top()[1]=t[2]+1;
                }
            }
            return time;
        }
        
        vector<int> parse(string& s) {
            vector<int> res;
            int ct=0;
            string temp;
            for(int i=0;i<s.size();i++) {
                if(s[i]==':') {
                    if(ct==0) res.push_back(stoi(temp));
                    else if(ct==1&&temp=="start") res.push_back(0);
                    else if(ct==1&&temp=="end") res.push_back(1);
                    temp.clear();
                    ct++;
                }
                else temp.append(1, s[i]);
            }
            res.push_back(stoi(temp));
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.