-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13317.java
More file actions
62 lines (54 loc) · 1.69 KB
/
13317.java
File metadata and controls
62 lines (54 loc) · 1.69 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
56
57
58
59
60
61
62
import java.util.*;
class Car{
int w;
int start;
public Car(int w, int start){
this.w = w;
this.start = start;
}
}
class Solution {
static Queue<Integer> wait = new LinkedList<>();
static Queue<Car> bridge = new LinkedList<>();
static boolean isAvailable(int bridge_length, int bridge_weight, int weight){
if(wait.peek() + bridge_weight <= weight && bridge.size() < bridge_length){
return true;
}
return false;
}
public int solution(int bridge_length, int weight, int[] truck_weights) {
for(int w : truck_weights){
wait.offer(w);
}
int car = wait.poll();
bridge.offer(new Car(car, 0));
int time = 1;
int bridge_weight = car;
while(!bridge.isEmpty()){
Car front = bridge.peek();
// 선두 트럭이 다리를 지날 수 있는지 확인
if(time - front.start == bridge_length){
bridge.poll();
bridge_weight -= front.w;
}
// 다리에 올라갈 수 있는지 확인
if(!wait.isEmpty()){
if(isAvailable(bridge_length, bridge_weight, weight)){
car = wait.poll();
bridge.offer(new Car(car, time));
bridge_weight += car;
}
}
time++;
}
return time;
}
}
/**
큐 이용
1. 대기 큐에 truck_weights 삽입
2. 큐가 빌때까지 반복문
- 대기 큐에서 꺼내서 다리 큐에 삽입, 이때 무게와 bridge_length 제한 확인
- 반복문 돌때마다 time 증가
O(10,000 * 10,000) = O(100,000,000)
*/