-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path636.Exclusive-Time-of-Functions.go
62 lines (55 loc) · 1.24 KB
/
636.Exclusive-Time-of-Functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://leetcode.com/problems/exclusive-time-of-functions/
//
// algorithms
// Medium (47.57%)
// Total Accepted: 32,290
// Total Submissions: 67,877
// beats 100.0% of golang submissions
package leetcode
import (
"strconv"
"strings"
)
type log struct {
id int
ts int
startOrEnd string
diff int
}
func exclusiveTime(n int, logs []string) []int {
res := make([]int, n)
var stack []log
for _, l := range logs {
logArr := strings.Split(l, ":")
id, _ := strconv.Atoi(logArr[0])
startOrEnd := logArr[1]
ts, _ := strconv.Atoi(logArr[2])
stackLen := len(stack)
if stackLen == 0 || id != stack[stackLen-1].id {
stack = append(stack, log{
id: id,
ts: ts,
startOrEnd: startOrEnd,
diff: 0,
})
} else {
if stack[stackLen-1].startOrEnd == "start" && startOrEnd == "end" {
duration := ts - stack[stackLen-1].ts + 1 - stack[stackLen-1].diff
res[id] += duration
diff := duration + stack[stackLen-1].diff
stack = stack[:stackLen-1]
if len(stack) > 0 {
stack[stackLen-2].diff += diff
}
} else {
stack = append(stack, log{
id: id,
ts: ts,
startOrEnd: startOrEnd,
diff: 0,
})
}
}
}
return res
}