Skip to content

Commit 30f15e2

Browse files
refactor 636
1 parent 65a8987 commit 30f15e2

File tree

1 file changed

+28
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-24
lines changed

‎src/main/java/com/fishercoder/solutions/_636.java

+28-24
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,35 @@
4141
1 <= n <= 100
4242
*/
4343
public class _636 {
44-
/**Based on the example, it's difficult to see how function 2 executes 4 units of time, actually
45-
* we can add 1 to all end times to make it easier to understand and AC'ed.*/
46-
public int[] exclusiveTime(int n, List<String> logs) {
47-
/**Stack is the way to go:
48-
* we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
49-
* we'll pop this logId only when we encounter this logId's end timestamp.
50-
* Meanwhile, we keep a counter called prevTime,
51-
* whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
52-
Deque<Integer> stack = new LinkedList<>();
53-
int[] result = new int[n];
54-
int prevTime = 0;
55-
for (String log : logs) {
56-
String[] parts = log.split(":");
57-
if (!stack.isEmpty()) {
58-
result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime;
59-
}
60-
prevTime = Integer.parseInt(parts[2]);
61-
if (parts[1].equals("start")) {
62-
stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push()
63-
} else {
64-
prevTime++;
65-
//remember to have result pluse 1 to match the problem AC criteria
66-
result[stack.pollFirst()]++;//i.e. stack.pop()
44+
public static class Solution1 {
45+
/**
46+
* Based on the example, it's difficult to see how function 2 executes 4 units of time, actually
47+
* we can add 1 to all end times to make it easier to understand and AC'ed.
48+
*/
49+
public int[] exclusiveTime(int n, List<String> logs) {
50+
/**Stack is the way to go:
51+
* we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
52+
* we'll pop this logId only when we encounter this logId's end timestamp.
53+
* Meanwhile, we keep a counter called prevTime,
54+
* whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
55+
Deque<Integer> stack = new LinkedList<>();
56+
int[] result = new int[n];
57+
int prevTime = 0;
58+
for (String log : logs) {
59+
String[] parts = log.split(":");
60+
if (!stack.isEmpty()) {
61+
result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime;
62+
}
63+
prevTime = Integer.parseInt(parts[2]);
64+
if (parts[1].equals("start")) {
65+
stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push()
66+
} else {
67+
prevTime++;
68+
//remember to have result pluse 1 to match the problem AC criteria
69+
result[stack.pollFirst()]++;//i.e. stack.pop()
70+
}
6771
}
72+
return result;
6873
}
69-
return result;
7074
}
7175
}

0 commit comments

Comments
 (0)