add new solutions

This commit is contained in:
kanna5 2026-01-27 18:38:43 +09:00
parent 67cad91898
commit 81cc2d3ba6
No known key found for this signature in database
19 changed files with 693 additions and 14 deletions

View 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