Fast & Easy java solution


  • 0
    S
        public int[] exclusiveTime(int n, List<String> logs) {
            int[] ans = new int[n];
            int index, time, lastTime = 0;
            Stack<Integer> stack = new Stack<>();
            
            for (String log : logs) {
                String[] parts = log.split(":");
                index = Integer.parseInt(parts[0]);
                time = Integer.parseInt(parts[2]);
                
                if (parts[1].equals("start")) {
                    if (!stack.isEmpty()) {
                        ans[stack.peek()] += time - lastTime;
                    }
                    stack.push(index);
                } else {
                    time++;
                    stack.pop();
                    ans[index] += time - lastTime;
                }
                lastTime = time;
            }
            return ans;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.