java.lang.Object
g0601_0700.s0636_exclusive_time_of_functions.Solution

public class Solution extends Object
636 - Exclusive Time of Functions.<p>Medium</p> <p>On a <strong>single-threaded</strong> CPU, we execute a program containing <code>n</code> functions. Each function has a unique ID between <code>0</code> and <code>n-1</code>.</p> <p>Function calls are <strong>stored in a <a href="https://en.wikipedia.org/wiki/Call_stack" target="_top">call stack</a></strong>: 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 <strong>the current function being executed</strong>. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.</p> <p>You are given a list <code>logs</code>, where <code>logs[i]</code> represents the <code>i<sup>th</sup></code> log message formatted as a string <code>&quot;{function_id}:{&quot;start&quot; | &quot;end&quot;}:{timestamp}&quot;</code>. For example, <code>&quot;0:start:3&quot;</code> means a function call with function ID <code>0</code> <strong>started at the beginning</strong> of timestamp <code>3</code>, and <code>&quot;1:end:2&quot;</code> means a function call with function ID <code>1</code> <strong>ended at the end</strong> of timestamp <code>2</code>. Note that a function can be called <strong>multiple times, possibly recursively</strong>.</p> <p>A function&rsquo;s <strong>exclusive time</strong> 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 <code>2</code> time units and another call executing for <code>1</code> time unit, the <strong>exclusive time</strong> is <code>2 + 1 = 3</code>.</p> <p>Return <em>the <strong>exclusive time</strong> of each function in an array, where the value at the</em> <code>i<sup>th</sup></code> <em>index represents the exclusive time for the function with ID</em> <code>i</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" alt="" /></p> <p><strong>Input:</strong> n = 2, logs = [&ldquo;0:start:0&rdquo;,&ldquo;1:start:2&rdquo;,&ldquo;1:end:5&rdquo;,&ldquo;0:end:6&rdquo;]</p> <p><strong>Output:</strong> [3,4]</p> <p><strong>Explanation:</strong></p> <p>Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.</p> <p>Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.</p> <p>Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.</p> <p>So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 1, logs = [&ldquo;0:start:0&rdquo;,&ldquo;0:start:2&rdquo;,&ldquo;0:end:5&rdquo;,&ldquo;0:start:6&rdquo;,&ldquo;0:end:6&rdquo;,&ldquo;0:end:7&rdquo;]</p> <p><strong>Output:</strong> [8]</p> <p><strong>Explanation:</strong></p> <p>Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.</p> <p>Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.</p> <p>Function 0 (initial call) resumes execution then immediately calls itself again.</p> <p>Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.</p> <p>Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.</p> <p>So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 2, logs = [&ldquo;0:start:0&rdquo;,&ldquo;0:start:2&rdquo;,&ldquo;0:end:5&rdquo;,&ldquo;1:start:6&rdquo;,&ldquo;1:end:6&rdquo;,&ldquo;0:end:7&rdquo;]</p> <p><strong>Output:</strong> [7,1]</p> <p><strong>Explanation:</strong></p> <p>Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.</p> <p>Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.</p> <p>Function 0 (initial call) resumes execution then immediately calls function 1.</p> <p>Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.</p> <p>Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.</p> <p>So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 100</code></li> <li><code>1 <= logs.length <= 500</code></li> <li><code>0 <= function_id < n</code></li> <li><code>0 <= timestamp <= 10<sup>9</sup></code></li> <li>No two start events will happen at the same timestamp.</li> <li>No two end events will happen at the same timestamp.</li> <li>Each function has an <code>&quot;end&quot;</code> log for each <code>&quot;start&quot;</code> log.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • exclusiveTime

      public int[] exclusiveTime(int n, List<String> logs)