|
41 | 41 | 1 <= n <= 100
|
42 | 42 | */
|
43 | 43 | publicclass_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 |
| -publicint[]exclusiveTime(intn,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 =newLinkedList<>(); |
53 |
| -int[]result =newint[n]; |
54 |
| -intprevTime =0; |
55 |
| -for (Stringlog :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 | +publicstaticclassSolution1 { |
| 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 | +publicint[]exclusiveTime(intn,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 =newLinkedList<>(); |
| 56 | +int[]result =newint[n]; |
| 57 | +intprevTime =0; |
| 58 | +for (Stringlog :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 | + } |
67 | 71 | }
|
| 72 | +returnresult; |
68 | 73 | }
|
69 |
| -returnresult; |
70 | 74 | }
|
71 | 75 | }
|