636. Exclusive Time of Functions

Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.

Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.

A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0" means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0.

Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function‘s exclusive time. You should return the exclusive time of each function sorted by their function id.

Example 1:

Input:
n = 2
logs =
["0:start:0",
 "1:start:2",
 "1:end:5",
 "0:end:6"]
Output:[3, 4]
Explanation:
Function 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1.
Now function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.
Function 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time.
So function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.
struct Log
{
    int id;
    string s;
    int time;
    Log(int _id,string _s,int _time)
    {
        id = _id;
        s = _s;
        time = _time;
    }
};

class Solution {
public:
    vector<int> exclusiveTime(int n, vector<string>& logs) {
        stack<Log> q;
        vector<int> res(n);
        for(auto log:logs)
        {
           vector<string> parsed = parse_log(log);
           int id =  stoll(parsed[0]);
           string s = parsed[1];
           int time = stoll(parsed[2]);
           Log curr_log(id,s,time);
           if(q.empty()||s=="start") q.push(curr_log);
           else
           {
               int curr_start_time = q.top().time;
               q.pop();
               int time_spent = time-curr_start_time+1;
               res[id] += time_spent;
                //下面这行的逻辑关系我刚开始没有想到,非常的优美。
                if(!q.empty())
                    res[q.top().id]= res[q.top().id]-time_spent;
           }
        }
        return res;
    }
private:
vector<string> parse_log(string log)
{
     vector<string>res;
     string token;
     log +=":";
     stringstream is(log);
     while(getline(is,token,‘:‘))
        res.push_back(token);
     return res;
}

};
时间: 2024-11-12 19:19:44

636. Exclusive Time of Functions的相关文章

[LeetCode] Exclusive Time of Functions 函数的独家时间

Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions. Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another functio

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

[leetcode-636-Exclusive Time of Functions]

Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions. Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another functio

Diesel-powered is famous globally among the the majority of exclusive view businesses

Individuals frequently believe that we now have a lot of view businesses close to these days, as well as even though this can be accurate for an degree, prior to the 50's, there have been so much more view businesses around. Certainly, the actual pla

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

POJ 1080 Human Gene Functions(LCS)

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their