-
Programmers LV.2 다리를 지나가는 트럭Programmers 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);
'Programmers' 카테고리의 다른 글
Programmers LV.2 더 맵게 (0) 2020.12.28 Programmers LV.2 프린터 (0) 2020.12.27 Programmers LV.2 주식가격 (0) 2020.12.20 Programmers LV.3 베스트앨범 (0) 2020.12.19 Programmers LV.2 위장 (0) 2020.12.17