본문 바로가기

카테고리 없음

프로그래머스>스택/큐>다리를 지나는 트럭

import java.util.Queue;
import java.util.LinkedList;

class Solution {
    public int solution(int bridge_length, int weight, int[] truck_weights) 
    {
        int answer = 0;
        
        Queue<Integer> bridge = new LinkedList<>();
        
        int current_weight= 0;
        for(int truck : truck_weights)
        {
            while(true)
            {
                if(bridge.isEmpty())
                {
                    bridge.offer(truck);
                    answer++; 
                    current_weight += truck;
                    break;
                    
                } else if(bridge.size() == bridge_length)
                {
                    current_weight -= bridge.poll();
                } else
                {
                    if(current_weight + truck > weight)
                 {
                    answer ++;    
                    bridge.offer(0);
                    
                }else 
                {
                    bridge.offer(truck);
                    current_weight += truck;
                    answer ++;
                    break;
                }
                    
                }
               }
            }
        
        return answer + bridge_length;
      }
 }