diff --git a/src/main/java/com/fishercoder/solutions/_636.java b/src/main/java/com/fishercoder/solutions/_636.java index bc19f179b6..34b36a5a11 100644 --- a/src/main/java/com/fishercoder/solutions/_636.java +++ b/src/main/java/com/fishercoder/solutions/_636.java @@ -41,31 +41,35 @@ 1 <= n <= 100 */ public class _636 { - /**Based on the example, it's difficult to see how function 2 executes 4 units of time, actually - * we can add 1 to all end times to make it easier to understand and AC'ed.*/ - public int[] exclusiveTime(int n, List logs) { - /**Stack is the way to go: - * we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp, - * we'll pop this logId only when we encounter this logId's end timestamp. - * Meanwhile, we keep a counter called prevTime, - * whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/ - Deque stack = new LinkedList<>(); - int[] result = new int[n]; - int prevTime = 0; - for (String log : logs) { - String[] parts = log.split(":"); - if (!stack.isEmpty()) { - result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime; - } - prevTime = Integer.parseInt(parts[2]); - if (parts[1].equals("start")) { - stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push() - } else { - prevTime++; - //remember to have result pluse 1 to match the problem AC criteria - result[stack.pollFirst()]++;//i.e. stack.pop() + public static class Solution1 { + /** + * Based on the example, it's difficult to see how function 2 executes 4 units of time, actually + * we can add 1 to all end times to make it easier to understand and AC'ed. + */ + public int[] exclusiveTime(int n, List logs) { + /**Stack is the way to go: + * we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp, + * we'll pop this logId only when we encounter this logId's end timestamp. + * Meanwhile, we keep a counter called prevTime, + * whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/ + Deque stack = new LinkedList<>(); + int[] result = new int[n]; + int prevTime = 0; + for (String log : logs) { + String[] parts = log.split(":"); + if (!stack.isEmpty()) { + result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime; + } + prevTime = Integer.parseInt(parts[2]); + if (parts[1].equals("start")) { + stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push() + } else { + prevTime++; + //remember to have result pluse 1 to match the problem AC criteria + result[stack.pollFirst()]++;//i.e. stack.pop() + } } + return result; } - return result; } }