Exclusive Time of Functions


"Approach #1 Using Stack [Time Limit Exceeded]"  looks misleading. The accepted solution is stackbased. Any approach can be misused. When you count using fingers instead of just adding/subtracting two numbers, you're doing just that  wasting time. You could have added Thread.Sleep(1000) as well.

Share my concise solution:
public int[] exclusiveTime(int n, List<String> logs) { int[] res = new int[n]; Stack<int[]> stack = new Stack<>(); for(String log : logs) { String[] s = log.split(":"); int idx = Integer.valueOf(s[0]); int t = Integer.valueOf(s[2]); if(s[1].equals("start")) { stack.push(new int[]{idx, t}); } else { int v = tstack.pop()[1]+1; res[idx] += v; if(!stack.empty()) res[stack.peek()[0]] = v; } } return res; }