当前位置:网站首页>Stack Title: exclusive time of function
Stack Title: exclusive time of function
2022-06-24 10:32:00 【The great Cherny】
List of articles
subject
Title and provenance
title : Exclusive time of function
Source :636. Exclusive time of function
difficulty
6 level
Title Description
requirement
There is one Single thread CPU Running a program containing n \texttt{n} n Program of trace function . Each function has a function located in 0 \texttt{0} 0 and n - 1 \texttt{n - 1} n - 1 Unique identifier between .
Function call Stored on a call stack : When a function call starts , Its identifier will be pushed onto the stack . And when a function call ends , Its identifier will pop up from the stack . The function with the identifier at the top of the stack is The currently executing function . Whenever a function starts or ends , A log will be recorded , Include function identifiers 、 Is it the beginning or the end 、 And the corresponding timestamp .
Give you a list of logs logs \texttt{logs} logs, among logs[i] \texttt{logs[i]} logs[i] It means the first one i \texttt{i} i Log messages , The message is a press "{function_id}:{"start" | "end"}:{timestamp}" \texttt{"\{function\_id\}:\{"start" | "end"\}:\{timestamp\}"} "{function_id}:{"start" | "end"}:{timestamp}" Formatted string . for example , "0:start:3" \texttt{"0:start:3"} "0:start:3" Means the identifier is 0 \texttt{0} 0 Function call in timestamp 3 \texttt{3} 3 Of Start execution ; and "1:end:2" \texttt{"1:end:2"} "1:end:2" Means the identifier is 1 \texttt{1} 1 Function call in timestamp 2 \texttt{2} 2 Of End execution . Be careful , A function can Call several times , There may be recursive calls .
Functional Exclusive time The definition is the sum of the execution time of this function in all function calls of the program , The time spent calling other functions does not count as the exclusive time of the function . for example , If a function is called twice , One call to execute 2 \texttt{2} 2 Unit time , Another call to execute 1 \texttt{1} 1 Unit time , Then the Exclusive time by 2 + 1 = 3 \texttt{2 + 1 = 3} 2 + 1 = 3.
Returns the of each function as an array Exclusive time , Among them the first i \texttt{i} i The value corresponding to each subscript represents the identifier i \texttt{i} i Exclusive time of function .
Example
Example 1:

Input : n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"] \texttt{n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]} n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
Output : [3,4] \texttt{[3,4]} [3,4]
explain :
function 0 \texttt{0} 0 At the time stamp 0 \texttt{0} 0 Start execution at the beginning of , perform 2 \texttt{2} 2 Unit time , Apply to timestamp 1 \texttt{1} 1 End of execution .
function 1 \texttt{1} 1 At the time stamp 2 \texttt{2} 2 Start execution at the beginning of , perform 4 \texttt{4} 4 Unit time , Apply to timestamp 5 \texttt{5} 5 End of execution .
function 0 \texttt{0} 0 At the time stamp 6 \texttt{6} 6 Resume execution at the beginning of , perform 1 \texttt{1} 1 Unit time .
So the function 0 \texttt{0} 0 Total execution 2 + 1 = 3 \texttt{2 + 1 = 3} 2 + 1 = 3 Unit time , function 1 \texttt{1} 1 Total execution 4 \texttt{4} 4 Unit time .
Example 2:
Input : n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"] \texttt{n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]} n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
Output : [8] \texttt{[8]} [8]
explain :
function 0 \texttt{0} 0 At the time stamp 0 \texttt{0} 0 Start execution at the beginning of , perform 2 \texttt{2} 2 Unit time , And recursively call itself .
function 0 \texttt{0} 0( Recursively call ) At the time stamp 2 \texttt{2} 2 Start execution at the beginning of , perform 4 \texttt{4} 4 Unit time .
function 0 \texttt{0} 0( Initial call ) Resume execution , And immediately call itself again .
function 0 \texttt{0} 0( The second recursive call ) At the time stamp 6 \texttt{6} 6 Start execution at the beginning of , perform 1 \texttt{1} 1 Unit time .
function 0 \texttt{0} 0( Initial call ) At the time stamp 7 \texttt{7} 7 The initial recovery execution of , perform 1 \texttt{1} 1 Unit time .
So the function 0 \texttt{0} 0 Total execution 2 + 4 + 1 + 1 = 8 \texttt{2 + 4 + 1 + 1 = 8} 2 + 4 + 1 + 1 = 8 Unit time .
Example 3:
Input : n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"] \texttt{n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]} n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
Output : [7,1] \texttt{[7,1]} [7,1]
explain :
function 0 \texttt{0} 0 At the time stamp 0 \texttt{0} 0 Start execution at the beginning of , perform 2 \texttt{2} 2 Unit time , And recursively call itself .
function 0 \texttt{0} 0( Recursively call ) At the time stamp 2 \texttt{2} 2 Start execution at the beginning of , perform 4 Unit time .
function 0 \texttt{0} 0( Initial call ) Resume execution , And immediately call the function 1 \texttt{1} 1.
function 1 \texttt{1} 1 At the time stamp 6 \texttt{6} 6 Start execution at the beginning of , perform 1 \texttt{1} 1 Unit time , Apply to timestamp 6 \texttt{6} 6 End of execution .
function 0 \texttt{0} 0( Initial call ) At the time stamp 7 \texttt{7} 7 The initial recovery execution of , perform 1 \texttt{1} 1 Unit time , Apply to timestamp 7 \texttt{7} 7 End of execution .
So the function 0 \texttt{0} 0 Total execution 2 + 4 + 1 = 7 \texttt{2 + 4 + 1 = 7} 2 + 4 + 1 = 7 Unit time , function 1 \texttt{1} 1 Total execution 1 \texttt{1} 1 Unit time .
Example 4:
Input : n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"] \texttt{n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"]} n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"]
Output : [8,1] \texttt{[8,1]} [8,1]
Example 5:
Input : n = 1, logs = ["0:start:0","0:end:0"] \texttt{n = 1, logs = ["0:start:0","0:end:0"]} n = 1, logs = ["0:start:0","0:end:0"]
Output : [1] \texttt{[1]} [1]
Data range
- 1 ≤ n ≤ 100 \texttt{1} \le \texttt{n} \le \texttt{100} 1≤n≤100
- 1 ≤ logs.length ≤ 500 \texttt{1} \le \texttt{logs.length} \le \texttt{500} 1≤logs.length≤500
- 0 ≤ function_id < n \texttt{0} \le \texttt{function\_id} < \texttt{n} 0≤function_id<n
- 0 ≤ timestamp ≤ 10 9 \texttt{0} \le \texttt{timestamp} \le \texttt{10}^\texttt{9} 0≤timestamp≤109
- Two start events do not occur at the same time stamp
- Two end events do not occur at the same timestamp
- Each function has a corresponding "start" \texttt{"start"} "start" The log "end" \texttt{"end"} "end" journal
solution
Ideas and algorithms
Because the method of function call is stored on the call stack , Therefore, the stack can be used to simulate the function call , The top element of the stack represents the identifier of the function being executed .
For a given log list , The storage order is in the order of timestamp , So you can traverse the log list , Calculate the exclusive time of each function . Each log contains three items of information , Namely : Function identifier 、 Start or end 、 Time stamp . When calculating function exclusive time , You need to consider the current timestamp and the previous timestamp , And the following different situations need to be considered :
The function starts executing , And the stack is empty , No other functions are executing at this time , So there is no need to update the exclusive time of any function , Stack the current function identifier ;
The function starts executing , And the stack is not empty , At this time, the original function corresponding to the stack top identifier is executing , The time after the new function starts to execute is not included in the exclusive time of the original function , Therefore, you need to update the exclusive time of the original function , Add the difference between the current timestamp and the previous timestamp to the exclusive time of the original function , Then put the current function identifier on the stack ;
The function ends execution , Because the current timestamp represents the end of the timestamp , Therefore, you need to add the timestamp 1 1 1, At this point, the identifier at the top of the stack must be the same as the current function identifier , The latest exclusive time of the current function is the current timestamp ( Has added 1 1 1) Difference from the previous timestamp , Add the difference between the current timestamp and the previous timestamp to the exclusive time of the current function , And push the top element out of the stack , If the stack is not empty after the top element is out of the stack , Then the new stack top element is the original function to restore the exclusive time .
After processing a log , Save the current timestamp as the previous timestamp , Then process the next log , Until all logs are processed , You can get the exclusive time of each function .
Example 3 The exclusive time of the function of is shown in the figure below .

The exclusive time of the function is calculated as follows . At the beginning time = [ 0 , 0 ] \textit{time} = [0, 0] time=[0,0].
[ 0:start:0 ] [\text{0:start:0}] [0:start:0]: Function identifier id = 0 \textit{id} = 0 id=0, Time stamp timestamp = 0 \textit{timestamp} = 0 timestamp=0, take 0 0 0 Push , time = [ 0 , 0 ] \textit{time} = [0, 0] time=[0,0];
[ 0:start:2 ] [\text{0:start:2}] [0:start:2]: Function identifier id = 0 \textit{id} = 0 id=0, Time stamp timestamp = 2 \textit{timestamp} = 2 timestamp=2, take 2 − 0 = 2 2 - 0 = 2 2−0=2 Add to time [ 0 ] \textit{time}[0] time[0], take 0 0 0 Push , time = [ 2 , 0 ] \textit{time} = [2, 0] time=[2,0];
[ 0:end:5 ] [\text{0:end:5}] [0:end:5]: Function identifier id = 0 \textit{id} = 0 id=0, Time stamp timestamp = 5 + 1 = 6 \textit{timestamp} = 5 + 1 = 6 timestamp=5+1=6, take 6 − 2 = 4 6 - 2 = 4 6−2=4 Add to time [ 0 ] \textit{time}[0] time[0], take 0 0 0 Out of the stack , time = [ 6 , 0 ] \textit{time} = [6, 0] time=[6,0];
[ 1:start:6 ] [\text{1:start:6}] [1:start:6]: Function identifier id = 1 \textit{id} = 1 id=1, Time stamp timestamp = 6 \textit{timestamp} = 6 timestamp=6, take 6 − 6 = 0 6 - 6 = 0 6−6=0 Add to time [ 0 ] \textit{time}[0] time[0], take 1 1 1 Push , time = [ 6 , 0 ] \textit{time} = [6, 0] time=[6,0];
[ 1:end:6 ] [\text{1:end:6}] [1:end:6]: Function identifier id = 1 \textit{id} = 1 id=1, Time stamp timestamp = 6 + 1 = 7 \textit{timestamp} = 6 + 1 = 7 timestamp=6+1=7, take 7 − 6 = 1 7 - 6 = 1 7−6=1 Add to time [ 1 ] \textit{time}[1] time[1], take 1 1 1 Out of the stack , time = [ 6 , 1 ] \textit{time} = [6, 1] time=[6,1];
[ 0:end:7 ] [\text{0:end:7}] [0:end:7]: Function identifier id = 0 \textit{id} = 0 id=0, Time stamp timestamp = 7 + 1 = 8 \textit{timestamp} = 7 + 1 = 8 timestamp=7+1=8, take 8 − 7 = 1 8 - 7 = 1 8−7=1 Add to time [ 0 ] \textit{time}[0] time[0], take 0 0 0 Out of the stack , time = [ 7 , 1 ] \textit{time} = [7, 1] time=[7,1].
Code
class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
int[] time = new int[n];
Deque<Integer> stack = new ArrayDeque<Integer>();
int prevTimestamp = 0;
int size = logs.size();
for (int i = 0; i < size; i++) {
String log = logs.get(i);
String[] logArray = log.split(":");
int id = Integer.parseInt(logArray[0]);
String action = logArray[1];
int timestamp = Integer.parseInt(logArray[2]);
if ("start".equals(action)) {
if (!stack.isEmpty()) {
int prevId = stack.peek();
time[prevId] += timestamp - prevTimestamp;
}
stack.push(id);
} else if ("end".equals(action)) {
timestamp++;
time[id] += timestamp - prevTimestamp;
stack.pop();
}
prevTimestamp = timestamp;
}
return time;
}
}
Complexity analysis
Time complexity : O ( m ) O(m) O(m), among m m m Is a list of logs logs \textit{logs} logs The length of . You need to traverse the log list once , The operation time for each log is O ( 1 ) O(1) O(1).
Spatial complexity : O ( m ) O(m) O(m), among m m m Is a list of logs logs \textit{logs} logs The length of . The space complexity mainly depends on the stack space , The stack stores the called function , The number of elements in the stack does not exceed m 2 \dfrac{m}{2} 2m. The return value is not included in the space complexity .
边栏推荐
- Leetcode - 498: traversée diagonale
- Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
- 3. addition, deletion, modification and query of employees
- 机械臂速成小指南(三):机械臂的机械结构
- JMeter接口测试工具基础— 取样器sampler(二)
- Six states of threads
- uniapp实现点击拨打电话功能
- 2022年能源与环境工程国际研讨会(CoEEE 2022)
- 用扫描的方法分发书稿校样
- Leetcode interview question 16.06: minimum difference
猜你喜欢
随机推荐
Leetcode interview question 01.05: primary editing
JMeter interface test tool foundation - use badboy to record JMeter script
【JS逆向分享】某个网站社区信息
leetCode-223: 矩形面积
3.员工的增删改查
Distributed transaction principle and solution
Leetcode-1823: find the winner of the game
Uniapp implements the function of clicking to make a call
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
线程池的状态
tf.errors
cuda runtime error (801) : Raw out
小程序 rich-text中图片点击放大与自适应大小问题
Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
5. dish management business development
线程池的执行流程
Sort out interface performance optimization skills and kill slow code
Uniapp implementation forbids video drag fast forward
[resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.







