1
+ package com .leetcode .stacks ;
2
+
3
+ import javafx .util .Pair ;
4
+
5
+ import java .util .Arrays ;
6
+ import java .util .List ;
7
+ import java .util .Stack ;
8
+
9
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
10
+
11
+ /**
12
+ * Level: Medium
13
+ * Link: https://leetcode.com/problems/exclusive-time-of-functions/
14
+ * Description:
15
+ * On a single threaded CPU, we execute some functions. Each function has a unique id between 0 and N-1.
16
+ *
17
+ * We store logs in timestamp order that describe when a function is entered or exited.
18
+ *
19
+ * Each log is a string with this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3"
20
+ * means the function with id 0 started at the beginning of timestamp 3. "1:end:2" means the function with id 1 ended
21
+ * at the end of timestamp 2.
22
+ *
23
+ * A function's exclusive time is the number of units of time spent in this function. Note that this does not include
24
+ * any recursive calls to child functions.
25
+ *
26
+ * The CPU is single threaded which means that only one function is being executed at a given time unit.
27
+ *
28
+ * Return the exclusive time of each function, sorted by their function id.
29
+ *
30
+ * Input:
31
+ * n = 2
32
+ * logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
33
+ * Output: [3, 4]
34
+ * Explanation:
35
+ * Function 0 starts at the beginning of time 0, then it executes 2 units of time and reaches the end of time 1.
36
+ * Now function 1 starts at the beginning of time 2, executes 4 units of time and ends at time 5.
37
+ * Function 0 is running again at the beginning of time 6, and also ends at the end of time 6, thus executing for 1 unit of time.
38
+ * So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
39
+ *
40
+ *
41
+ * Note:
42
+ * -> 1 <= n <= 100
43
+ * -> Two functions won't start or end at the same time.
44
+ * -> Functions will always log when they exit.
45
+ *
46
+ * @author rampatra
47
+ * @since 2019-08-17
48
+ */
49
+ public class ExclusiveTimeOfFunctions {
50
+
51
+ /**
52
+ * Runtime: <a href="https://leetcode.com/submissions/detail/252509066/">18 ms</a>.
53
+ *
54
+ * @param n
55
+ * @param logs
56
+ * @return
57
+ */
58
+ public static int [] exclusiveTime (int n , List <String > logs ) {
59
+ int [] times = new int [n ];
60
+ Stack <Pair <Integer , Integer >> stack = new Stack <>();
61
+
62
+ for (String log : logs ) {
63
+ String [] l = log .split (":" );
64
+ int id = Integer .parseInt (l [0 ]);
65
+ String operation = l [1 ];
66
+ int timestamp = Integer .parseInt (l [2 ]);
67
+
68
+ if (operation .equals ("start" )) {
69
+ if (!stack .empty ()) {
70
+ times [stack .peek ().getKey ()] += (timestamp - stack .peek ().getValue () - 1 );
71
+ }
72
+ stack .push (new Pair <>(id , timestamp ));
73
+ } else {
74
+ times [id ] += timestamp - stack .pop ().getValue () + 1 ;
75
+ if (!stack .isEmpty ()) {
76
+ stack .push (new Pair <>(stack .pop ().getKey (), timestamp ));
77
+ }
78
+ }
79
+ }
80
+
81
+ return times ;
82
+ }
83
+
84
+ public static void main (String [] args ) {
85
+ assertEquals ("[4]" , Arrays .toString (exclusiveTime (1 , Arrays .asList ("0:start:0" , "0:start:1" , "0:end:2" , "0:end:3" ))));
86
+ assertEquals ("[6]" , Arrays .toString (exclusiveTime (1 , Arrays .asList ("0:start:0" , "0:start:1" , "0:start:2" , "0:end:3" , "0:end:4" , "0:end:5" ))));
87
+ assertEquals ("[3, 4]" , Arrays .toString (exclusiveTime (2 , Arrays .asList ("0:start:0" , "1:start:2" , "1:end:5" , "0:end:6" ))));
88
+ }
89
+ }
0 commit comments