func exclusiveTime(_ n: Int, _ logs: [String]) -> [Int] {
var answer = Array(repeating: 0, count: n)
var stack = [Int]()
var pretime = 0
for i in stride(from: 0, to: logs.count, by: 1) {
let components = logs[i].components(separatedBy: ":")
let function = Int(components[0])!
let timestamp = Int(components[2])!
if stack.isEmpty {
stack.append(function)
pretime = timestamp
} else {
if components[1] == "start" {
let top = stack.last!
answer[top] += (timestamp - pretime)
pretime = timestamp
stack.append(function)
} else {
let top = stack.removeLast()
answer[top] += (timestamp - pretime + 1)
pretime = timestamp + 1
}
}
}
return answer
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme