add new solutions
This commit is contained in:
parent
67cad91898
commit
eb6ffe8114
24 changed files with 933 additions and 14 deletions
35
solutions/6/q636/solution.go
Normal file
35
solutions/6/q636/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q636
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func exclusiveTime(n int, logs []string) []int {
|
||||
ret := make([]int, n)
|
||||
stack := make([][3]int, 0, 256) // proc id, start time, child exec time
|
||||
|
||||
for i := range logs {
|
||||
fields := strings.FieldsFunc(logs[i], func(r rune) bool { return r == ':' })
|
||||
id, _ := strconv.ParseInt(fields[0], 10, strconv.IntSize)
|
||||
action := fields[1]
|
||||
curTime, _ := strconv.ParseInt(fields[2], 10, strconv.IntSize)
|
||||
|
||||
switch action {
|
||||
case "start":
|
||||
stack = append(stack, [3]int{int(id), int(curTime), 0})
|
||||
case "end":
|
||||
i := len(stack) - 1
|
||||
id, stT, chT := stack[i][0], stack[i][1], stack[i][2]
|
||||
ret[id] += int(curTime) - stT + 1 - chT
|
||||
|
||||
if len(stack) > 1 {
|
||||
stack[i-1][2] += int(curTime) - stT + 1
|
||||
}
|
||||
stack = stack[:len(stack)-1] // pop
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = exclusiveTime
|
||||
Loading…
Add table
Add a link
Reference in a new issue