Skip to content

Commit 21ff909

Browse files
update 636
1 parent 7684774 commit 21ff909

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_636.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,27 @@
66

77
public class _636 {
88
public static class Solution1 {
9-
/**
10-
* Based on the example, it's difficult to see how function 2 executes 4 units of time, actually
11-
* we can add 1 to all end times to make it easier to understand and AC'ed.
12-
*/
139
public int[] exclusiveTime(int n, List<String> logs) {
1410
/**Stack is the way to go:
15-
* we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
16-
* we'll pop this logId only when we encounter this logId's end timestamp.
17-
* Meanwhile, we keep a counter called prevTime,
18-
* whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
11+
* 1. we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
12+
* 2. we'll pop this logId only when we encounter this logId's end timestamp.
13+
* 3. Meanwhile, we keep a counter called prevTime,
14+
* 4. whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
1915
Deque<Integer> stack = new LinkedList<>();
2016
int[] result = new int[n];
2117
int prevTime = 0;
2218
for (String log : logs) {
2319
String[] parts = log.split(":");
2420
if (!stack.isEmpty()) {
25-
result[stack.peek()] += Integer.parseInt(parts[2]) - prevTime;
21+
result[stack.peekLast()] += Integer.parseInt(parts[2]) - prevTime;
2622
}
2723
prevTime = Integer.parseInt(parts[2]);
2824
if (parts[1].equals("start")) {
29-
stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push()
25+
stack.addLast(Integer.parseInt(parts[0]));
3026
} else {
27+
//remember to have result plus 1, i.e. when a task starts at 2, ends at 5, it should be 5 -2 + 1 = 4
3128
prevTime++;
32-
//remember to have result pluse 1 to match the problem AC criteria
33-
result[stack.pollFirst()]++;//i.e. stack.pop()
29+
result[stack.pollLast()]++;
3430
}
3531
}
3632
return result;

Diff for: src/test/java/com/fishercoder/_636Test.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._636;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
10+
11+
public class _636Test {
12+
private static _636.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _636.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertArrayEquals(new int[]{3, 4}, solution1.exclusiveTime(2, Arrays.asList("0:start:0", "1:start:2", "1:end:5", "0:end:6")));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)