Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. class Solution {
  2.     public int[] exclusiveTime(int n, List<String> logs) {
  3.         int[] res = new int[n];
  4.         Stack<int[]> stack = new Stack<>();
  5.         for (String tag: logs) {
  6.             String[] tokens = tag.split(":");
  7.             Integer p = Integer.parseInt(tokens[0]), t = Integer.parseInt(tokens[2]);
  8.             if (tokens[1].equals("start")) {
  9.                 stack.push(new int[]{p, t});
  10.             } else {
  11.                 int[] last = stack.pop();
  12.                 int dur = t - last[1] + 1;
  13.                 res[p] += dur;
  14.                 if (!stack.isEmpty())
  15.                     res[stack.peek()[0]] -= dur;
  16.             }
  17.         }
  18.        
  19.         return res;
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement