Skip to content

Commit de5c710

Browse files
committed
Sync LeetCode submission Runtime - 8 ms (35.78%), Memory - 17.9 MB (35.90%)
1 parent 139d807 commit de5c710

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<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>
2+
3+
<p>Function calls are <strong>stored in a <a href="https://en.wikipedia.org/wiki/Call_stack">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>
4+
5+
<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 <b>multiple times, possibly recursively</b>.</p>
6+
7+
<p>A function&#39;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>
8+
9+
<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>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" style="width: 550px; height: 239px;" />
14+
<pre>
15+
<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;1:start:2&quot;,&quot;1:end:5&quot;,&quot;0:end:6&quot;]
16+
<strong>Output:</strong> [3,4]
17+
<strong>Explanation:</strong>
18+
Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
19+
Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
20+
Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
21+
So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> n = 1, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;0:start:6&quot;,&quot;0:end:6&quot;,&quot;0:end:7&quot;]
28+
<strong>Output:</strong> [8]
29+
<strong>Explanation:</strong>
30+
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
31+
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
32+
Function 0 (initial call) resumes execution then immediately calls itself again.
33+
Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
34+
Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
35+
So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.
36+
</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;1:start:6&quot;,&quot;1:end:6&quot;,&quot;0:end:7&quot;]
42+
<strong>Output:</strong> [7,1]
43+
<strong>Explanation:</strong>
44+
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
45+
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
46+
Function 0 (initial call) resumes execution then immediately calls function 1.
47+
Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.
48+
Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.
49+
So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= n &lt;= 100</code></li>
57+
<li><code>1 &lt;= logs.length &lt;= 500</code></li>
58+
<li><code>0 &lt;= function_id &lt; n</code></li>
59+
<li><code>0 &lt;= timestamp &lt;= 10<sup>9</sup></code></li>
60+
<li>No two start events will happen at the same timestamp.</li>
61+
<li>No two end events will happen at the same timestamp.</li>
62+
<li>Each function has an <code>&quot;end&quot;</code> log for each <code>&quot;start&quot;</code> log.</li>
63+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 2: Better Stack Approach
2+
3+
# n = len(logs)
4+
# Time: O(n)
5+
# Space: O(n)
6+
7+
class Solution:
8+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
9+
stack = []
10+
res = [0] * n
11+
12+
# process first log entry
13+
s = logs[0].split(':')
14+
stack.append(int(s[0]))
15+
prev = int(s[2])
16+
17+
i = 1
18+
while i < len(logs):
19+
s = logs[i].split(':')
20+
curr_time = int(s[2])
21+
22+
if s[1] == 'start':
23+
if stack:
24+
res[stack[-1]] += curr_time - prev
25+
stack.append(int(s[0]))
26+
prev = curr_time
27+
else:
28+
res[stack[-1]] += curr_time - prev + 1
29+
stack.pop()
30+
prev = curr_time + 1
31+
32+
i += 1
33+
34+
return res
35+

0 commit comments

Comments
 (0)