-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion3.java
More file actions
55 lines (52 loc) · 1.92 KB
/
Copy pathQuestion3.java
File metadata and controls
55 lines (52 loc) · 1.92 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
48
49
50
51
52
53
54
55
package practice3_struct;
import java.util.*;
public class Question3 {
// 현관문 출입 순서
public int[] solution(int[] arrival, int[] state){
Queue<Integer> enter = new LinkedList<>();
Queue<Integer> exit = new LinkedList<>();
int n = arrival.length, prev = 1;
int[] answer = new int[n];
for(int t = 0, i = 0, cnt = 0; ; t++){
if(enter.isEmpty() && exit.isEmpty() && i < n) {
if(t < arrival[i]){
t = arrival[i];
prev = 1;
}
}
while(i < n && arrival[i] <= t) {
if (state[i] == 0) enter.offer(i);
else exit.offer(i);
i++;
}
if(prev == 1) {
if(!exit.isEmpty()) {
answer[exit.poll()] = t;
prev = 1;
}
else{
answer[enter.poll()] = t;
prev = 0;
}
}else if(prev == 0) {
if(!enter.isEmpty()) {
answer[enter.poll()] = t;
prev = 0;
}else{
answer[exit.poll()] = t;
prev = 1;
}
}
cnt++;
if(cnt == n) break;
}
return answer;
}
// 도착순서면 거의 큐를 사용하고, 도착, 출발이있다면 2개를 사용해야함.
public static void main(String[] args){
Question3 T = new Question3();
System.out.println(Arrays.toString(T.solution(new int[]{0, 1, 1, 1, 2, 3, 8, 8}, new int[]{1, 0, 0, 1, 0, 0, 1, 0})));
System.out.println(Arrays.toString(T.solution(new int[]{3, 3, 4, 5, 5, 5}, new int[]{1, 0, 1, 0, 1, 0})));
System.out.println(Arrays.toString(T.solution(new int[]{2, 2, 2, 3, 4, 8, 8, 9, 10, 10}, new int[]{1, 0, 0, 0, 1, 1, 0, 1, 1, 0})));
}
}