Leetcode 636: Exclusive Time of Functions

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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.

 

Note:

  1. Input logs will be sorted by timestamp, NOT log id.
  2. Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
  3. Two functions won‘t start or end at the same time.
  4. Functions could be called recursively, and will always end.
  5. 1 <= n <= 100
 1 public class Solution {
 2     public int[] ExclusiveTime(int n, IList<string> logs) {
 3         var result = new int[n];
 4         var stack = new Stack<int>();
 5         int prevTime = 0;
 6              
 7         foreach (var logStr in logs)
 8         {
 9             var array = logStr.Split(:);
10             
11             if (stack.Count != 0)
12             { 
13                 // this is the execlusive time that doesn‘t contain the last minute
14                 result[stack.Peek()] += Int32.Parse(array[2]) - prevTime;
15             }
16             
17             prevTime = Int32.Parse(array[2]);
18             
19             if (array[1] == "start")
20             {
21                 stack.Push(Int32.Parse(array[0]));
22             }
23             else
24             {
25                 // we need to do +1 because we don‘t include the last minute on line #13
26                 result[stack.Pop()]++;
27                 
28                 // since the last minute belongs to above function, the new function starts at the next minute
29                 prevTime++;
30             }
31             
32         }
33                      
34         return result;
35     }
36 }
37 
38 public class Solution1 {
39     public class Log
40     {
41         public int id;
42         public int timestamp;
43         public bool isStart;
44         
45         public Log(int id, int t, bool start)
46         {
47             this.id = id;
48             this.timestamp = t;
49             this.isStart = start;
50         }
51     }
52     
53     private Log ParseLog(string log)
54     {
55         var array = log.Split(:);
56         
57         return new Log(Int32.Parse(array[0]), Int32.Parse(array[2]), array[1] == "start");
58     }
59     
60     public int[] ExclusiveTime(int n, IList<string> logs) {
61         var result = new int[n];
62         var stack = new Stack<Log>();
63         
64         foreach (var logStr in logs)
65         {
66             var log = ParseLog(logStr);
67             
68             if (stack.Count == 0)
69             {
70                 stack.Push(log);
71             }
72             // there are two cases we need to push the current log : 1. a new function (different id) call 2. a recursive call of the same               // function 
73             else if (log.id != stack.Peek().id || (log.id == stack.Peek().id && log.isStart))
74             {
75                 result[stack.Peek().id] += log.timestamp - stack.Peek().timestamp;
76                 stack.Peek().timestamp = log.timestamp + 1;
77                 stack.Push(log);
78             }
79             // this is the case we see the end entry of a function call, the current stack top must be the start
80             else
81             {
82                 result[stack.Peek().id] += log.timestamp - stack.Peek().timestamp + 1;
83                 stack.Pop();
84                 
85                 if (stack.Count > 0)
86                 {
87                     stack.Peek().timestamp = log.timestamp + 1;
88                 }
89             }
90         }
91                      
92         return result;
93     }
94 }

 

以上是关于Leetcode 636: Exclusive Time of Functions的主要内容,如果未能解决你的问题,请参考以下文章

636. Exclusive Time of Functions

636. Exclusive Time of Functions

636. Exclusive Time of Functions

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

Leetcode 636.函数的独占时间

DateFormatter 格式:2014-12-08T14:11:32.636Z