package OneDayOneCodingChallenge;
import static org.junit.Assert.assertEquals;
import java.util.LinkedList;
import java.util.Queue;
import org.junit.Test;
public class Day6 {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int answer = 0;
Queue<Integer> trucksOnBridge = new LinkedList<>();
int sum = 0;
int time = 0;
for (int t : truck_weights) {
while (true) {
time++;
if (trucksOnBridge.size() == bridge_length) {
sum -= trucksOnBridge.poll();
}
if (trucksOnBridge.isEmpty()) {
sum = t;
trucksOnBridge.add(t);
break;
} else if (t + sum <= weight) { // 트럭이 다리위를 올라갈 수 있는 상태
sum = sum + t;
trucksOnBridge.add(t);
break;
} else { //트럭이 다리위를 올라갈 수 없는 상황
trucksOnBridge.add(0);
}
}
}
return time+bridge_length; //마지막으로 Queue에 들어간 트럭이 건너는 길이를 더해줌
}
@Test
public void 정답() {
assertEquals(8, solution(2, 10, new int[]{7, 4, 5, 6}));
}
}
'Algorithm' 카테고리의 다른 글
[프로그래머스]위장 (0) | 2020.09.14 |
---|---|
[프로그래머스]문자열 내 마음대로 정렬하기 (0) | 2020.09.11 |
[프로그래머스]기능개발 (0) | 2020.09.08 |
[프로그래머스]라면공장 문제풀이(우선순위큐) (0) | 2020.09.07 |
[프로그래머스]네트워크 문제풀이(DFS와 BFS) (0) | 2020.09.05 |