-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion8.java
More file actions
47 lines (43 loc) · 1.62 KB
/
Copy pathQuestion8.java
File metadata and controls
47 lines (43 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package practice1_Impl;
import java.util.Arrays;
public class Question8 {
public int[] solution(int[] enter, int[] exit){
int n = enter.length;
for(int i = 0; i < n; i++){
enter[i]--;
exit[i]--;
}
int[] enterIdx = new int[n];
for(int i = 0; i < n; i++){
enterIdx[enter[i]] = i;
}
int[] enterT = new int[n];
int[] exitT = new int[n];
int cnt = 0;
for(int i = 0, j = 0; i < n; i++){
while(j < n && j <= enterIdx[exit[i]]){
enterT[enter[j]] = cnt++;
j++;
}
exitT[exit[i]] = cnt++;
}
int[] answer = new int[n];
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(!(exitT[i] < enterT[j] || exitT[j] < enterT[i])){
answer[i]++;
answer[j]++;
}
}
}
return answer;
}
public static void main(String[] args){
Question8 T = new Question8();
System.out.println(Arrays.toString(T.solution(new int[]{1, 2, 3, 4}, new int[]{2, 4, 1, 3})));
System.out.println(Arrays.toString(T.solution(new int[]{1, 2, 5, 3, 4}, new int[]{2, 3, 1, 4, 5})));
System.out.println(Arrays.toString(T.solution(new int[]{1, 3, 2, 4, 5, 7, 6, 8}, new int[]{2, 3, 5, 6, 1, 4, 8, 7})));
System.out.println(Arrays.toString(T.solution(new int[]{1, 4, 7, 2, 3, 5, 6}, new int[]{5, 2, 6, 1, 7, 3, 4})));
System.out.println(Arrays.toString(T.solution(new int[]{1, 4, 2, 3}, new int[]{2, 1, 4, 3})));
}
}