@@ -34,10 +34,10 @@ tags:
3434<strong >输入:</strong >n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
3535<strong >输出:</strong >[3,4]
3636<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 个单位时间。
4141</pre >
4242
4343<p ><strong >示例 2:</strong ></p >
8787
8888<!-- solution:start -->
8989
90- ### 方法一:栈模拟
90+ ### 方法一:栈 + 模拟
9191
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$ 为日志数组的长度。
93101
94102<!-- tabs:start -->
95103
@@ -98,22 +106,20 @@ tags:
98106``` python
99107class Solution :
100108 def exclusiveTime (self , n : int , logs : List[str ]) -> List[int ]:
101- ans = [0 ] * n
102109 stk = []
103- curr = - 1
110+ ans = [0 ] * n
111+ pre = 0
104112 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" :
109116 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
113120 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
117123 return ans
118124```
119125
@@ -124,21 +130,20 @@ class Solution {
124130 public int [] exclusiveTime (int n , List<String > logs ) {
125131 int [] ans = new int [n];
126132 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 ' ) {
133139 if (! stk. isEmpty()) {
134- ans[stk. peek()] += ts - curr ;
140+ ans[stk. peek()] += cur - pre ;
135141 }
136- stk. push(fid );
137- curr = ts ;
142+ stk. push(i );
143+ pre = cur ;
138144 } 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 ;
142147 }
143148 }
144149 return ans;
@@ -154,20 +159,21 @@ public:
154159 vector<int > exclusiveTime(int n, vector<string >& logs) {
155160 vector<int > ans(n);
156161 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;
166173 } else {
167- fid = stk.top();
174+ ans [ stk.top()] += cur - pre + 1 ;
168175 stk.pop();
169- ans[ fid] += ts - curr + 1;
170- curr = ts + 1;
176+ pre = cur + 1;
171177 }
172178 }
173179 return ans;
@@ -181,22 +187,21 @@ public:
181187func exclusiveTime(n int, logs []string) []int {
182188 ans := make([]int, n)
183189 stk := []int{}
184- curr := 1
190+ pre := 0
185191 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' {
190196 if len(stk) > 0 {
191- ans[stk[len(stk)-1]] += ts - curr
197+ ans[stk[len(stk)-1]] += cur - pre
192198 }
193- stk = append(stk, fid )
194- curr = ts
199+ stk = append(stk, i )
200+ pre = cur
195201 } else {
196- fid := stk[len(stk)-1]
202+ ans[ stk[len(stk)-1]] += cur - pre + 1
197203 stk = stk[:len(stk)-1]
198- ans[fid] += ts - curr + 1
199- curr = ts + 1
204+ pre = cur + 1
200205 }
201206 }
202207 return ans
@@ -207,29 +212,23 @@ func exclusiveTime(n int, logs []string) []int {
207212
208213``` ts
209214function 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 [] = [];
213218 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 ;
221223 }
222- stack .push ([id , time ]);
224+ stk .push (+ i );
225+ pre = + cur ;
223226 } 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 ;
229229 }
230230 }
231-
232- return res ;
231+ return ans ;
233232}
234233```
235234
0 commit comments