|
| 1 | +/** |
| 2 | +* Given the running logs of n functions that are executed in a nonpreemptive |
| 3 | +* single threaded CPU, find the exclusive time of these functions. |
| 4 | +* |
| 5 | +* Each function has a unique id, start from 0 to n-1. A function may be called |
| 6 | +* recursively or by another function. |
| 7 | +* |
| 8 | +* A log is a string has this format : function_id:start_or_end:timestamp. For |
| 9 | +* example, "0:start:0" means function 0 starts from the very beginning of |
| 10 | +* time 0. "0:end:0" means function 0 ends to the very end of time 0. |
| 11 | +* |
| 12 | +* Exclusive time of a function is defined as the time spent within this |
| 13 | +* function, the time spent by calling other functions should not be considered |
| 14 | +* as this function's exclusive time. You should return the exclusive time of |
| 15 | +* each function sorted by their function id. |
| 16 | +* |
| 17 | +* Example 1: |
| 18 | +* Input: |
| 19 | +* n = 2 |
| 20 | +* logs = |
| 21 | +* ["0:start:0", |
| 22 | +* "1:start:2", |
| 23 | +* "1:end:5", |
| 24 | +* "0:end:6"] |
| 25 | +* Output:[3, 4] |
| 26 | +* |
| 27 | +* Explanation: |
| 28 | +* Function 0 starts at time 0, then it executes 2 units of time and reaches |
| 29 | +* the end of time 1. |
| 30 | +* Now function 0 calls function 1, function 1 starts at time 2, executes 4 |
| 31 | +* units of time and end at time 5. |
| 32 | +* Function 0 is running again at time 6, and also end at the time 6, thus |
| 33 | +* executes 1 unit of time. |
| 34 | +* So function 0 totally execute 2 + 1 = 3 units of time, and function 1 |
| 35 | +* totally execute 4 units of time. |
| 36 | +* |
| 37 | +* Note: |
| 38 | +* Input logs will be sorted by timestamp, NOT log id. |
| 39 | +* Your output should be sorted by function id, which means the 0th element of |
| 40 | +* your output corresponds to the exclusive time of function 0. |
| 41 | +* Two functions won't start or end at the same time. |
| 42 | +* Functions could be called recursively, and will always end. |
| 43 | +* 1 <= n <= 100 |
| 44 | +*/ |
| 45 | + |
| 46 | +public class ExclusiveTimeOfFunctions636 { |
| 47 | +public int[] exclusiveTime(int n, List<String> logs) { |
| 48 | +int[] res = new int[n]; |
| 49 | +Stack<Integer> actives = new Stack<>(); |
| 50 | +int preTime = 0; |
| 51 | +boolean preIsStart = true; |
| 52 | +for (String log: logs) { |
| 53 | +String[] strs = log.split(":"); |
| 54 | +int id = Integer.valueOf(strs[0]); |
| 55 | +boolean isStart = strs[1].equals("start"); |
| 56 | +int now = Integer.valueOf(strs[2]); |
| 57 | +if (isStart) { |
| 58 | +if (!actives.isEmpty()) { |
| 59 | +res[actives.peek()] += now - preTime - (preIsStart ? 0 : 1); |
| 60 | +} |
| 61 | +actives.push(id); |
| 62 | +} else { |
| 63 | +res[actives.peek()] += now - preTime + (preIsStart ? 1 : 0); |
| 64 | +actives.pop(); |
| 65 | +} |
| 66 | +preTime = now; |
| 67 | +preIsStart = isStart; |
| 68 | +} |
| 69 | +return res; |
| 70 | +} |
| 71 | + |
| 72 | +} |
0 commit comments