636. Exclusive Time of Functions Medium

@problem@discussion
#Array#Stack



1/**
2 * [636] Exclusive Time of Functions
3 *
4 * On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1.
5 * Function calls are stored in a <a href="https://en.wikipedia.org/wiki/Call_stack">call stack</a>: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.
6 * You are given a list logs, where logs[i] represents the i^th log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended at the end of timestamp 2. Note that a function can be called multiple times, possibly recursively.
7 * A function's exclusive time is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for 2 time units and another call executing for 1 time unit, the exclusive time is 2 + 1 = 3.
8 * Return the exclusive time of each function in an array, where the value at the i^th index represents the exclusive time for the function with ID i.
9 *  
10 * Example 1:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" style="width: 550px; height: 239px;" />
12 * Input: n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
13 * Output: [3,4]
14 * Explanation:
15 * Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
16 * Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
17 * Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
18 * So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
19 * 
20 * Example 2:
21 * 
22 * Input: n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
23 * Output: [8]
24 * Explanation:
25 * Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
26 * Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
27 * Function 0 (initial call) resumes execution then immediately calls itself again.
28 * Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
29 * Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
30 * So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.
31 * 
32 * Example 3:
33 * 
34 * Input: n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
35 * Output: [7,1]
36 * Explanation:
37 * Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
38 * Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
39 * Function 0 (initial call) resumes execution then immediately calls function 1.
40 * Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.
41 * Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.
42 * So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.
43 * 
44 *  
45 * Constraints:
46 * 
47 * 	1 <= n <= 100
48 * 	1 <= logs.length <= 500
49 * 	0 <= function_id < n
50 * 	0 <= timestamp <= 10^9
51 * 	No two start events will happen at the same timestamp.
52 * 	No two end events will happen at the same timestamp.
53 * 	Each function has an "end" log for each "start" log.
54 * 
55 */
56pub struct Solution {}
57
58// problem: https://leetcode.com/problems/exclusive-time-of-functions/
59// discuss: https://leetcode.com/problems/exclusive-time-of-functions/discuss/?currentPage=1&orderBy=most_votes&query=
60
61// submission codes start here
62
63impl Solution {
64    pub fn exclusive_time(n: i32, logs: Vec<String>) -> Vec<i32> {
65        vec![]
66    }
67}
68
69// submission codes end
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_636() {
77    }
78}
79


Back
© 2025 bowen.ge All Rights Reserved.