Skip to content

Commit 583bcfb

Browse files
committed
feat: add solutions to lc problem: No.0636
No.0636.Exclusive Time of Functions
1 parent bd281b2 commit 583bcfb

File tree

6 files changed

+306
-4
lines changed

6 files changed

+306
-4
lines changed

solution/0600-0699/0636.Exclusive Time of Functions/README.md

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,67 @@
8888

8989
<!-- 这里可写通用的实现逻辑 -->
9090

91+
**方法一:栈模拟**
92+
93+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
94+
9195
<!-- tabs:start -->
9296

9397
### **Python3**
9498

9599
<!-- 这里可写当前语言的特殊实现逻辑 -->
96100

97101
```python
98-
102+
class Solution:
103+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
104+
ans = [0] * n
105+
stk = []
106+
curr = -1
107+
for log in logs:
108+
t = log.split(':')
109+
fid = int(t[0])
110+
ts = int(t[2])
111+
if t[1] == 'start':
112+
if stk:
113+
ans[stk[-1]] += ts - curr
114+
stk.append(fid)
115+
curr = ts
116+
else:
117+
fid = stk.pop()
118+
ans[fid] += ts - curr + 1
119+
curr = ts + 1
120+
return ans
99121
```
100122

101123
### **Java**
102124

103125
<!-- 这里可写当前语言的特殊实现逻辑 -->
104126

105127
```java
106-
128+
class Solution {
129+
public int[] exclusiveTime(int n, List<String> logs) {
130+
int[] ans = new int[n];
131+
Deque<Integer> stk = new ArrayDeque<>();
132+
int curr = -1;
133+
for (String log : logs) {
134+
String[] t = log.split(":");
135+
int fid = Integer.parseInt(t[0]);
136+
int ts = Integer.parseInt(t[2]);
137+
if ("start".equals(t[1])) {
138+
if (!stk.isEmpty()) {
139+
ans[stk.peek()] += ts - curr;
140+
}
141+
stk.push(fid);
142+
curr = ts;
143+
} else {
144+
fid = stk.pop();
145+
ans[fid] += ts - curr + 1;
146+
curr = ts + 1;
147+
}
148+
}
149+
return ans;
150+
}
151+
}
107152
```
108153

109154
### **TypeScript**
@@ -136,6 +181,67 @@ function exclusiveTime(n: number, logs: string[]): number[] {
136181
}
137182
```
138183

184+
### **C++**
185+
186+
```cpp
187+
class Solution {
188+
public:
189+
vector<int> exclusiveTime(int n, vector<string>& logs) {
190+
vector<int> ans(n);
191+
stack<int> stk;
192+
int curr = -1;
193+
for (auto& log : logs)
194+
{
195+
char type[10];
196+
int fid, ts;
197+
sscanf(log.c_str(), "%d:%[^:]:%d", &fid, type, &ts);
198+
if (type[0] == 's')
199+
{
200+
if (!stk.empty()) ans[stk.top()] += ts - curr;
201+
curr = ts;
202+
stk.push(fid);
203+
}
204+
else
205+
{
206+
fid = stk.top();
207+
stk.pop();
208+
ans[fid] += ts - curr + 1;
209+
curr = ts + 1;
210+
}
211+
}
212+
return ans;
213+
}
214+
};
215+
```
216+
217+
### **Go**
218+
219+
```go
220+
func exclusiveTime(n int, logs []string) []int {
221+
ans := make([]int, n)
222+
stk := []int{}
223+
curr := 1
224+
for _, log := range logs {
225+
t := strings.Split(log, ":")
226+
fid, _ := strconv.Atoi(t[0])
227+
ts, _ := strconv.Atoi(t[2])
228+
if t[1][0] == 's' {
229+
if len(stk) > 0 {
230+
ans[stk[len(stk)-1]] += ts - curr
231+
}
232+
stk = append(stk, fid)
233+
curr = ts
234+
} else {
235+
fid := stk[len(stk)-1]
236+
stk = stk[:len(stk)-1]
237+
ans[fid] += ts - curr + 1
238+
curr = ts + 1
239+
}
240+
}
241+
return ans
242+
}
243+
```
244+
139245
### **...**
140246

141247
```

solution/0600-0699/0636.Exclusive Time of Functions/README_EN.md

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,54 @@ So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1
7575
### **Python3**
7676

7777
```python
78-
78+
class Solution:
79+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
80+
ans = [0] * n
81+
stk = []
82+
curr = -1
83+
for log in logs:
84+
t = log.split(':')
85+
fid = int(t[0])
86+
ts = int(t[2])
87+
if t[1] == 'start':
88+
if stk:
89+
ans[stk[-1]] += ts - curr
90+
stk.append(fid)
91+
curr = ts
92+
else:
93+
fid = stk.pop()
94+
ans[fid] += ts - curr + 1
95+
curr = ts + 1
96+
return ans
7997
```
8098

8199
### **Java**
82100

83101
```java
84-
102+
class Solution {
103+
public int[] exclusiveTime(int n, List<String> logs) {
104+
int[] ans = new int[n];
105+
Deque<Integer> stk = new ArrayDeque<>();
106+
int curr = -1;
107+
for (String log : logs) {
108+
String[] t = log.split(":");
109+
int fid = Integer.parseInt(t[0]);
110+
int ts = Integer.parseInt(t[2]);
111+
if ("start".equals(t[1])) {
112+
if (!stk.isEmpty()) {
113+
ans[stk.peek()] += ts - curr;
114+
}
115+
stk.push(fid);
116+
curr = ts;
117+
} else {
118+
fid = stk.pop();
119+
ans[fid] += ts - curr + 1;
120+
curr = ts + 1;
121+
}
122+
}
123+
return ans;
124+
}
125+
}
85126
```
86127

87128
### **TypeScript**
@@ -114,6 +155,67 @@ function exclusiveTime(n: number, logs: string[]): number[] {
114155
}
115156
```
116157

158+
### **C++**
159+
160+
```cpp
161+
class Solution {
162+
public:
163+
vector<int> exclusiveTime(int n, vector<string>& logs) {
164+
vector<int> ans(n);
165+
stack<int> stk;
166+
int curr = -1;
167+
for (auto& log : logs)
168+
{
169+
char type[10];
170+
int fid, ts;
171+
sscanf(log.c_str(), "%d:%[^:]:%d", &fid, type, &ts);
172+
if (type[0] == 's')
173+
{
174+
if (!stk.empty()) ans[stk.top()] += ts - curr;
175+
curr = ts;
176+
stk.push(fid);
177+
}
178+
else
179+
{
180+
fid = stk.top();
181+
stk.pop();
182+
ans[fid] += ts - curr + 1;
183+
curr = ts + 1;
184+
}
185+
}
186+
return ans;
187+
}
188+
};
189+
```
190+
191+
### **Go**
192+
193+
```go
194+
func exclusiveTime(n int, logs []string) []int {
195+
ans := make([]int, n)
196+
stk := []int{}
197+
curr := 1
198+
for _, log := range logs {
199+
t := strings.Split(log, ":")
200+
fid, _ := strconv.Atoi(t[0])
201+
ts, _ := strconv.Atoi(t[2])
202+
if t[1][0] == 's' {
203+
if len(stk) > 0 {
204+
ans[stk[len(stk)-1]] += ts - curr
205+
}
206+
stk = append(stk, fid)
207+
curr = ts
208+
} else {
209+
fid := stk[len(stk)-1]
210+
stk = stk[:len(stk)-1]
211+
ans[fid] += ts - curr + 1
212+
curr = ts + 1
213+
}
214+
}
215+
return ans
216+
}
217+
```
218+
117219
### **...**
118220

119221
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> exclusiveTime(int n, vector<string>& logs) {
4+
vector<int> ans(n);
5+
stack<int> stk;
6+
int curr = -1;
7+
for (auto& log : logs)
8+
{
9+
char type[10];
10+
int fid, ts;
11+
sscanf(log.c_str(), "%d:%[^:]:%d", &fid, type, &ts);
12+
if (type[0] == 's')
13+
{
14+
if (!stk.empty()) ans[stk.top()] += ts - curr;
15+
curr = ts;
16+
stk.push(fid);
17+
}
18+
else
19+
{
20+
fid = stk.top();
21+
stk.pop();
22+
ans[fid] += ts - curr + 1;
23+
curr = ts + 1;
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func exclusiveTime(n int, logs []string) []int {
2+
ans := make([]int, n)
3+
stk := []int{}
4+
curr := 1
5+
for _, log := range logs {
6+
t := strings.Split(log, ":")
7+
fid, _ := strconv.Atoi(t[0])
8+
ts, _ := strconv.Atoi(t[2])
9+
if t[1][0] == 's' {
10+
if len(stk) > 0 {
11+
ans[stk[len(stk)-1]] += ts - curr
12+
}
13+
stk = append(stk, fid)
14+
curr = ts
15+
} else {
16+
fid := stk[len(stk)-1]
17+
stk = stk[:len(stk)-1]
18+
ans[fid] += ts - curr + 1
19+
curr = ts + 1
20+
}
21+
}
22+
return ans
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] exclusiveTime(int n, List<String> logs) {
3+
int[] ans = new int[n];
4+
Deque<Integer> stk = new ArrayDeque<>();
5+
int curr = -1;
6+
for (String log : logs) {
7+
String[] t = log.split(":");
8+
int fid = Integer.parseInt(t[0]);
9+
int ts = Integer.parseInt(t[2]);
10+
if ("start".equals(t[1])) {
11+
if (!stk.isEmpty()) {
12+
ans[stk.peek()] += ts - curr;
13+
}
14+
stk.push(fid);
15+
curr = ts;
16+
} else {
17+
fid = stk.pop();
18+
ans[fid] += ts - curr + 1;
19+
curr = ts + 1;
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
3+
ans = [0] * n
4+
stk = []
5+
curr = -1
6+
for log in logs:
7+
t = log.split(':')
8+
fid = int(t[0])
9+
ts = int(t[2])
10+
if t[1] == 'start':
11+
if stk:
12+
ans[stk[-1]] += ts - curr
13+
stk.append(fid)
14+
curr = ts
15+
else:
16+
fid = stk.pop()
17+
ans[fid] += ts - curr + 1
18+
curr = ts + 1
19+
return ans

0 commit comments

Comments
 (0)