码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 636: Exclusive Time of Functions

时间:2018-01-28 11:28:45      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:pre   sort   ++   efi   unit   unique   called   log   output   

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

标签:pre   sort   ++   efi   unit   unique   called   log   output   

原文地址:https://www.cnblogs.com/liangmou/p/8370360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!