Programmers

Programmers LV.2 다리를 지나가는 트럭

Developer Garam.Choi 2020. 12. 23. 23:49

 

 

처음엔 큐를 많이 사용하지 않다버릇하니깐 까먹은 부분이 존재해서 다시 확인 후 개발 하는 작업을 진행하였다.

 

@org.junit.Test
    public void testBridge(){
        int bridge_length = 100;
        int weight = 100;
        int[] truck_weights ={10,10,10,10,10,10,10,10,10,10};
/*
        int bridge_length = 100;
        int weight = 100;
        int[] truck_weights ={10};
*/
/*
        int bridge_length = 2;
        int weight = 10;
        int[] truck_weights ={7,4,5,6};
*/

        Queue<Integer> queue = new LinkedList<>();

        int time = 0;
        int max = 0;
        for(int a=0; a < truck_weights.length; a++) {
            if(queue.isEmpty()){
                queue.offer(truck_weights[a]);
                time++;
                max = truck_weights[a];
            }else if(queue.size() < bridge_length){
                if(weight >= (max+truck_weights[a])){
                    queue.offer(truck_weights[a]);
                    time++;
                    max = max+truck_weights[a];
                }else{
                    time++;
                    max = max - queue.poll();
                    if(weight >= (max+truck_weights[a])){
                        queue.offer(truck_weights[a]);
                        time++;
                        max = max+truck_weights[a];
                    }else{
                        time++;
                        max = max - queue.poll();
                        queue.offer(truck_weights[a]);
                        time++;
                        max = max+truck_weights[a];
                    }
                }
            }else{
                max = max - queue.poll();
                time++;
                queue.offer(truck_weights[a]);
                time++;
                max = max+truck_weights[a];
            }
        }

        System.out.println(time+bridge_length);


    }

 

 

정말 헷갈리네.. 지문이 다 이상하다

 

 Queue<Integer> queue = new LinkedList<>();

        int time = 0;
        int max = 0;
        for(int a=0; a < truck_weights.length; a++) {
            while (true){
                if(queue.isEmpty()){
                    queue.offer(truck_weights[a]);
                    time++;
                    max = truck_weights[a];
                    break;
                }else if(queue.size() == bridge_length){
                    max -= queue.poll();
                }else{
                    if(weight < (max+truck_weights[a])){
                        time++;
                        queue.offer(0);
                    }else{
                        queue.offer(truck_weights[a]);
                        time++;
                        max += truck_weights[a];
                        break;
                    }
                }
            }
        }

        System.out.println(time + bridge_length);