var exclusiveTime = function(n, logs) {
const r = new Array(n).fill(0), m = logs.length, st = []
let prevTime = 0, prevSign = ''
for (let i = 0; i < m; i++) {
const t = logs[i].split(':'), cur = +t[0], sign = t[1], time = +t[2]
if (sign === 'start') {
if (st.length) r[st[st.length - 1]] += time - prevTime - (prevSign === 'start' ^ 1)
st.push(cur)
} else {
r[cur] += time - prevTime + (prevSign === 'end' ^ 1)
st.pop()
}
prevTime = time
prevSign = sign
}
return r
};
var exclusiveTime = function(n, logs) { // 虚拟栈 · 指针模拟栈
const r = new Array(n).fill(0), m = logs.length, st = new Array(m)
let prevTime = 0, prevSign = '', j = 0
for (let i = 0; i < m; i++) {
const t = logs[i].split(':'), cur = +t[0], sign = t[1], time = +t[2]
if (sign === 'start') {
if (j > 0) r[st[j - 1]] += time - prevTime - (prevSign === 'start' ^ 1)
st[j++] = cur
} else {
r[cur] += time - prevTime + (prevSign === 'end' ^ 1)
j--
}
prevTime = time
prevSign = sign
}
return r
};
function exclusiveTime(n: number, logs: string[]): number[] {
const r = new Array(n).fill(0), m = logs.length, st = []
let prevSign = '', prevTime = 0
for (let i = 0; i < m; i++) {
const t = logs[i].split(':'), cur = +t[0], sign = t[1], time = +t[2]
if (sign === 'start') {
if (st.length) r[st[st.length - 1]] += time - prevTime - (prevSign === 'start' ? 0 : 1)
st.push(cur)
} else {
r[cur] += time - prevTime + (prevSign === 'start' ? 1 : 0)
st.pop()
}
prevSign = sign
prevTime = time
}
return r
};
func exclusiveTime(n int, logs []string) []int {
r, prevSign, prevTime, st := make([]int, n), "", 0, []int{}
for _, log := range logs {
t := strings.Split(log, ":")
cur, _ := strconv.Atoi(t[0])
sign := t[1]
time, _ := strconv.Atoi(t[2])
if sign == "start" {
if len(st) > 0 {
r[st[len(st) - 1]] += time - prevTime - btoi(prevSign == "end")
}
st = append(st, cur)
} else {
r[cur] += time - prevTime + btoi(prevSign == "start")
st = st[:len(st) - 1]
}
prevSign = sign
prevTime = time
}
return r
}
func btoi (b bool) int {
if b {
return 1
}
return 0
}
class Solution {
public int[] exclusiveTime(int n, List<String> logs) {
int[] r = new int[n];
Deque<Integer> st = new ArrayDeque<Integer>();
String prevSign = "";
int prevTime = 0;
for (String log : logs) {
String[] t = log.split(":");
int cur = Integer.parseInt(t[0]);
String sign = t[1];
int time = Integer.parseInt(t[2]);
if (sign.equals("start")) {
if (st.isEmpty() == false) r[st.peek()] += time - prevTime - btoi(prevSign.equals("end"));
st.push(cur);
} else {
r[cur] += time - prevTime + btoi(prevSign.equals("start"));
if (st.isEmpty() == false) st.pop();
}
prevSign = sign;
prevTime = time;
}
return r;
}
private int btoi (boolean b) {
return b ? 1 : 0;
}
}
public class Solution {
public int[] ExclusiveTime(int n, IList<string> logs) {
int[] r = new int[n];
Stack<int> st = new Stack<int>();
string prevSign = "";
int prevTime = 0;
foreach(string log in logs) {
string[] t = log.Split(":");
int cur = int.Parse(t[0]);
string sign = t[1];
int time = int.Parse(t[2]);
if (sign == "start") {
if (st.Count > 0) r[st.Peek()] += time - prevTime - Atoi(prevSign == "end");
st.Push(cur);
} else {
r[cur] += time - prevTime + Atoi(prevSign == "start");
st.Pop();
}
prevSign = sign;
prevTime = time;
}
return r;
}
private int Atoi(bool b) {
return b ? 1 : 0;
}
}
static inline int btoi (bool b) {
return b ? 1 : 0;
}
int* exclusiveTime(int n, char ** logs, int logsSize, int* returnSize){
int* r = malloc(sizeof(int) * n);
memset(r, 0, sizeof(int) * n);
int* st = malloc(sizeof(int) * logsSize);
char prevSign;
int prevTime = 0, head = 0;
for (int i = 0; i < logsSize; i++) {
char sign[6];
int cur, time;
sscanf(logs[i], "%d:%[^:]:%d", &cur, sign, &time);
if (sign[0] == 's') {
if (head > 0) r[st[head - 1]] += time - prevTime - btoi(prevSign == 'e');
st[head++] = cur;
} else {
r[cur] += time - prevTime + btoi(prevSign == 's');
head--;
}
prevSign = sign[0];
prevTime = time;
}
free(st);
*returnSize = n;
return r;
}
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string>& logs) {
vector<int> r(n, 0);
stack<int> st;
char prevSign;
int prevTime = 0;
for (string& log : logs) {
int cur, time;
char sign[6];
sscanf(log.c_str(), "%d:%[^:]:%d", &cur, sign, &time);
if (sign[0] == 's') {
if (st.empty() == false) r[st.top()] += time - prevTime - (prevSign == 'e');
st.push(cur);
} else {
r[cur] += time - prevTime + (prevSign == 's');
st.pop();
}
prevSign = sign[0];
prevTime = time;
}
return r;
}
};
class Solution:
def exclusiveTime(self, n: int, logs: List[str]) -> List[int]:
r, st, prevSign, prevTime = [0] * n, [], "", 0
for log in logs:
cur, sign, time = log.split(':')
cur, time = int(cur), int(time)
if sign == "start":
if len(st): r[st[len(st) - 1]] += time - prevTime - int(prevSign == "end")
st.append(cur)
else:
r[cur] += time - prevTime + int(prevSign == "start")
st.pop()
prevSign = sign
prevTime = time
return r