zoukankan      html  css  js  c++  java
  • 636. Exclusive Time of Functions

    // TODO: need improve!!!
    class Log {
    public:
        int id;
        bool start;
        int timestamp;
        int comp;   // compasation
        Log() {
            id = 0;
            timestamp = 0;
            comp = 0;
            start = false;
        }
    };
    class Solution {
    public:
        vector<int> exclusiveTime(int n, vector<string>& logs) {
            vector<int> res(n, 0);
            stack<Log> s;
            for (auto &log : logs) {
                auto item = parseLog(log);
                if (item.start)
                    s.push(item);
                else {
                    int comp = item.timestamp - s.top().timestamp + 1;
                    int exTime = item.timestamp - s.top().timestamp + 1 - s.top().comp;
                    res[item.id] += exTime;
                    s.pop();
                    if (!s.empty()) {
                        s.top().comp += comp;
                    }
                }
            }
            return res;
        }
        Log parseLog(const string& s) {
            int col1 = s.find(":");
            int col2 = s.find(":", col1 + 1);
            int id = stoi(s.substr(0, col1));
            int time = stoi(s.substr(col2 + 1));
            string state = s.substr(col1 + 1, col2 - col1 - 1);
            
            Log log;
            if (state == "start")
                log.start = true;
            log.id = id;
            log.timestamp = time;
                
            return log;
        }
    };
  • 相关阅读:
    分解质因数
    第十一次作业
    第十次作业
    第九次作业
    第八次作业
    Radar Installation
    Stall Reservations
    Sunscreen
    天才ACM
    奇数码问题
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9111015.html
Copyright © 2011-2022 走看看