import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
/**
* @author xnl
* @Description:
* @date: 2022/8/7 22:16
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public int[] exclusiveTime(int n, List<String> logs) {
int[] res = new int[n];
Deque<int[]> stack = new ArrayDeque<>();
for (String log : logs) {
// 运行的线程
int idx = Integer.parseInt(log.substring(0, log.indexOf(':')));
// 运行的类型,有两种,一种是start,一种end
String type = log.substring(log.indexOf(':') + 1, log.lastIndexOf(':') );
// 对应的时间戳
int timestamp = Integer.parseInt(log.substring(log.lastIndexOf(':') + 1));
if (type.equals("start")){
if (!stack.isEmpty()){
// 保存下上一个线程的时间
res[stack.peek()[0]] += timestamp - stack.peek()[1];
}
// 记录当先线程的开始时间
stack.push(new int[]{idx, timestamp});
} else {
int[] t= stack.poll();
// 记录下这个线程的运行时间
res[t[0]] += timestamp - t[1] + 1;
if (!stack.isEmpty()){
stack.peek()[1] = timestamp + 1;
}
}
}
return res;
}
}