Created
January 18, 2020 06:00
-
-
Save dalcon10028/9d7150efc6ddccaac8f3910237094b74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| /** | |
| * Solution | |
| */ | |
| public class Solution { | |
| public static void main(String[] args) { | |
| int bridge_length = 2; | |
| int weight = 10; | |
| int[] truck_weights = {7, 4, 5, 6}; | |
| System.out.print(solution(bridge_length, weight, truck_weights)); | |
| } | |
| public static int solution(int bridge_length, int weight, int[] truck_weights) { | |
| int answer = 1; // 경과시간 1부터 | |
| ArrayList<Integer> truck = new ArrayList<>(); // 대기 트럭 | |
| ArrayList<int[]> onBridge = new ArrayList<>(); // 다리를 건너는 트럭, 트럭당 걸리는 시간 | |
| for (int i : truck_weights) truck.add(i); // 트럭 무게를 대기 트럭 리스트에 넣기 | |
| onBridge.add(new int[]{truck.get(0), bridge_length}); truck.remove(0); // 첫 번째 트럭이 다리에 들어서고 시작 | |
| while(!onBridge.isEmpty()){ // 다리위에 트럭이 없을 때까지 | |
| System.out.println("waitingTruck" + truck); | |
| for (int[] is : onBridge) | |
| System.out.println("time : " + answer + " truck : " + is[0] + " restTime : " + is[1]); | |
| if(onBridge.get(0)[1]==1) // 다리에 있는 가장 먼저 들어간 트럭이 남은 거리가 1이면 | |
| onBridge.remove(0); // 다리를 통과하기 | |
| for (int[] i : onBridge) // 다리에 트럭들은 | |
| i[1]--; //남은거리 줄이기 | |
| if (!truck.isEmpty()) { // 대기하고 있는 트럭이 있으면 | |
| int sum=truck.get(0); // 대기하고 있는 트럭과 | |
| for (int[] i : onBridge) sum+=i[0]; // 다리위의 트럭의 무게들의 합이 | |
| if(sum <= weight){ // 기준보다 작다면 | |
| onBridge.add(new int[]{truck.get(0), bridge_length}); // 다리에 진입하기 | |
| truck.remove(0); | |
| } | |
| } | |
| answer++; // 시간의 경과 | |
| } | |
| return answer; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment