본문 바로가기
프로그래머스/코딩연습2

[프로그래머스] 다리를 지나는 트럭(42583)(java)

by 유줘니 2019. 5. 29.

원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42587

문제 풀이 : https://developerdk.tistory.com/16

 

 

import java.util.Iterator;
import java.util.LinkedList;

class Truck {
	int weight;
	int distance;
	Truck(int weight, int distance){
		this.weight = weight;
		this.distance = distance;
	}
}
class Solution {
public static int solution(int bridge_length, int weight, int[] truck_weights) {
		int count = 0;
		LinkedList<Truck> loaded_truck = new LinkedList<Truck>();
		LinkedList<Truck> truck_queue = new LinkedList<Truck>();
		
		for(int truck_weight : truck_weights)
			truck_queue.add(new Truck(truck_weight, bridge_length));
		
		while(!(truck_queue.isEmpty()&&loaded_truck.isEmpty())) {
			count++;
            
			if(!loaded_truck.isEmpty()&&loaded_truck.getFirst().distance == 0) {
				weight += loaded_truck.getFirst().weight;
				loaded_truck.poll();
			}
			
			if(!truck_queue.isEmpty()&&weight - truck_queue.getFirst().weight >= 0) {
				weight -= truck_queue.getFirst().weight;
				loaded_truck.add(truck_queue.poll());
			}
            
            Iterator iter = loaded_truck.iterator();
			while(iter.hasNext()) {
				Truck truck = (Truck)iter.next();
				truck.distance--;
			}
		}
		return count;
	}
}

'프로그래머스 > 코딩연습2' 카테고리의 다른 글

[프로그래머스] 프린터  (0) 2019.05.29
[프로그래머스] 실패율 (42889)(java)  (0) 2019.05.28

댓글