@@ -34,10 +34,10 @@ tags:
34
34
<strong >输入:</strong >n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
35
35
<strong >输出:</strong >[3,4]
36
36
<strong >解释:</strong >
37
- 函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,于时间戳 1 的末尾结束执行。
38
- 函数 1 在时间戳 2 的起始开始执行,执行 4 个单位时间,于时间戳 5 的末尾结束执行。
39
- 函数 0 在时间戳 6 的开始恢复执行,执行 1 个单位时间。
40
- 所以函数 0 总共执行 2 + 1 = 3 个单位时间,函数 1 总共执行 4 个单位时间。
37
+ 函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,于时间戳 1 的末尾结束执行。
38
+ 函数 1 在时间戳 2 的起始开始执行,执行 4 个单位时间,于时间戳 5 的末尾结束执行。
39
+ 函数 0 在时间戳 6 的开始恢复执行,执行 1 个单位时间。
40
+ 所以函数 0 总共执行 2 + 1 = 3 个单位时间,函数 1 总共执行 4 个单位时间。
41
41
</pre >
42
42
43
43
<p ><strong >示例 2:</strong ></p >
87
87
88
88
<!-- solution:start -->
89
89
90
- ### 方法一:栈模拟
90
+ ### 方法一:栈 + 模拟
91
91
92
- 时间复杂度 $O(n)$,空间复杂度 $O(n)$。
92
+ 我们定义一个栈 $\textit{stk}$,用于存储当前正在执行的函数的标识符。同时,我们定义一个数组 $\textit{ans}$,用于存储每个函数的独占时间,初始时每个函数的独占时间都为 $0$。用一个变量 $\textit{pre}$ 记录上一个时间戳。
93
+
94
+ 遍历日志数组,对于每一条日志,我们首先将其按照冒号分隔,得到函数标识符 $\textit{i}$,操作类型 $\textit{op}$ 和时间戳 $\textit{t}$。
95
+
96
+ 如果 $\textit{op}$ 为 $\text{start}$,则表示函数 $\textit{i}$ 开始执行,我们需要判断栈是否为空,如果不为空,则将栈顶函数的独占时间增加 $\textit{cur} - \textit{pre}$,然后将 $\textit{i}$ 入栈,更新 $\textit{pre}$ 为 $\textit{cur}$;如果 $\textit{op}$ 为 $\text{end}$,则表示函数 $\textit{i}$ 结束执行,我们将栈顶函数的独占时间增加 $\textit{cur} - \textit{pre} + 1$,然后将栈顶元素出栈,更新 $\textit{pre}$ 为 $\textit{cur} + 1$。
97
+
98
+ 最后返回数组 $\textit{ans}$ 即可。
99
+
100
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为日志数组的长度。
93
101
94
102
<!-- tabs:start -->
95
103
@@ -98,22 +106,20 @@ tags:
98
106
``` python
99
107
class Solution :
100
108
def exclusiveTime (self , n : int , logs : List[str ]) -> List[int ]:
101
- ans = [0 ] * n
102
109
stk = []
103
- curr = - 1
110
+ ans = [0 ] * n
111
+ pre = 0
104
112
for log in logs:
105
- t = log.split(' :' )
106
- fid = int (t[0 ])
107
- ts = int (t[2 ])
108
- if t[1 ] == ' start' :
113
+ i, op, t = log.split(" :" )
114
+ i, cur = int (i), int (t)
115
+ if op[0 ] == " s" :
109
116
if stk:
110
- ans[stk[- 1 ]] += ts - curr
111
- stk.append(fid )
112
- curr = ts
117
+ ans[stk[- 1 ]] += cur - pre
118
+ stk.append(i )
119
+ pre = cur
113
120
else :
114
- fid = stk.pop()
115
- ans[fid] += ts - curr + 1
116
- curr = ts + 1
121
+ ans[stk.pop()] += cur - pre + 1
122
+ pre = cur + 1
117
123
return ans
118
124
```
119
125
@@ -124,21 +130,20 @@ class Solution {
124
130
public int [] exclusiveTime (int n , List<String > logs ) {
125
131
int [] ans = new int [n];
126
132
Deque<Integer > stk = new ArrayDeque<> ();
127
- int curr = - 1 ;
128
- for (String log : logs) {
129
- String [] t = log. split(" :" );
130
- int fid = Integer . parseInt(t [0 ]);
131
- int ts = Integer . parseInt(t [2 ]);
132
- if (" start " . equals(t [1 ]) ) {
133
+ int pre = 0 ;
134
+ for (var log : logs) {
135
+ var parts = log. split(" :" );
136
+ int i = Integer . parseInt(parts [0 ]);
137
+ int cur = Integer . parseInt(parts [2 ]);
138
+ if (parts [1 ]. charAt( 0 ) == ' s ' ) {
133
139
if (! stk. isEmpty()) {
134
- ans[stk. peek()] += ts - curr ;
140
+ ans[stk. peek()] += cur - pre ;
135
141
}
136
- stk. push(fid );
137
- curr = ts ;
142
+ stk. push(i );
143
+ pre = cur ;
138
144
} else {
139
- fid = stk. pop();
140
- ans[fid] += ts - curr + 1 ;
141
- curr = ts + 1 ;
145
+ ans[stk. pop()] += cur - pre + 1 ;
146
+ pre = cur + 1 ;
142
147
}
143
148
}
144
149
return ans;
@@ -154,20 +159,21 @@ public:
154
159
vector<int > exclusiveTime(int n, vector<string >& logs) {
155
160
vector<int > ans(n);
156
161
stack<int > stk;
157
- int curr = -1;
158
- for (auto& log : logs) {
159
- char type[ 10] ;
160
- int fid, ts;
161
- sscanf(log.c_str(), "%d:%[ ^ : ] :%d", &fid, type, &ts);
162
- if (type[ 0] == 's') {
163
- if (!stk.empty()) ans[ stk.top()] += ts - curr;
164
- curr = ts;
165
- stk.push(fid);
162
+ int pre = 0;
163
+ for (const auto& log : logs) {
164
+ int i, cur;
165
+ char c[ 10] ;
166
+ sscanf(log.c_str(), "%d:%[ ^ : ] :%d", &i, c, &cur);
167
+ if (c[ 0] == 's') {
168
+ if (stk.size()) {
169
+ ans[ stk.top()] += cur - pre;
170
+ }
171
+ stk.push(i);
172
+ pre = cur;
166
173
} else {
167
- fid = stk.top();
174
+ ans [ stk.top()] += cur - pre + 1 ;
168
175
stk.pop();
169
- ans[ fid] += ts - curr + 1;
170
- curr = ts + 1;
176
+ pre = cur + 1;
171
177
}
172
178
}
173
179
return ans;
@@ -181,22 +187,21 @@ public:
181
187
func exclusiveTime(n int, logs []string) []int {
182
188
ans := make([]int, n)
183
189
stk := []int{}
184
- curr := 1
190
+ pre := 0
185
191
for _, log := range logs {
186
- t := strings.Split(log, ":")
187
- fid , _ := strconv.Atoi(t [0])
188
- ts , _ := strconv.Atoi(t [2])
189
- if t [1][0] == 's' {
192
+ parts := strings.Split(log, ":")
193
+ i , _ := strconv.Atoi(parts [0])
194
+ cur , _ := strconv.Atoi(parts [2])
195
+ if parts [1][0] == 's' {
190
196
if len(stk) > 0 {
191
- ans[stk[len(stk)-1]] += ts - curr
197
+ ans[stk[len(stk)-1]] += cur - pre
192
198
}
193
- stk = append(stk, fid )
194
- curr = ts
199
+ stk = append(stk, i )
200
+ pre = cur
195
201
} else {
196
- fid := stk[len(stk)-1]
202
+ ans[ stk[len(stk)-1]] += cur - pre + 1
197
203
stk = stk[:len(stk)-1]
198
- ans[fid] += ts - curr + 1
199
- curr = ts + 1
204
+ pre = cur + 1
200
205
}
201
206
}
202
207
return ans
@@ -207,29 +212,23 @@ func exclusiveTime(n int, logs []string) []int {
207
212
208
213
``` ts
209
214
function exclusiveTime(n : number , logs : string []): number [] {
210
- const res = new Array (n ).fill (0 );
211
- const stack : [ number , number ][] = [] ;
212
-
215
+ const ans : number [] = Array (n ).fill (0 );
216
+ let pre = 0 ;
217
+ const stk : number [] = [];
213
218
for (const log of logs ) {
214
- const t = log .split (' :' );
215
- const [id, state, time] = [Number (t [0 ]), t [1 ], Number (t [2 ])];
216
-
217
- if (state === ' start' ) {
218
- if (stack .length !== 0 ) {
219
- const pre = stack [stack .length - 1 ];
220
- res [pre [0 ]] += time - pre [1 ];
219
+ const [i, op, cur] = log .split (' :' );
220
+ if (op [0 ] === ' s' ) {
221
+ if (stk .length ) {
222
+ ans [stk .at (- 1 )! ] += + cur - pre ;
221
223
}
222
- stack .push ([id , time ]);
224
+ stk .push (+ i );
225
+ pre = + cur ;
223
226
} else {
224
- const pre = stack .pop ();
225
- res [pre [0 ]] += time - pre [1 ] + 1 ;
226
- if (stack .length !== 0 ) {
227
- stack [stack .length - 1 ][1 ] = time + 1 ;
228
- }
227
+ ans [stk .pop ()! ] += + cur - pre + 1 ;
228
+ pre = + cur + 1 ;
229
229
}
230
230
}
231
-
232
- return res ;
231
+ return ans ;
233
232
}
234
233
```
235
234
0 commit comments