Leetcode 636: Exclusive Time of Functions

本文介绍了一种算法,用于计算在单线程CPU上运行的多个函数的独占时间。通过对函数调用日志进行解析,并利用堆栈来跟踪函数的调用与返回过程,最终实现了准确地计算每个函数的实际执行时间。

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 }

 

转载于:https://www.cnblogs.com/liangmou/p/8370360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值