diff --git a/src/week11/sunghee/p1/RefactorSolution.java b/src/week11/sunghee/p1/RefactorSolution.java new file mode 100644 index 0000000..3d2238e --- /dev/null +++ b/src/week11/sunghee/p1/RefactorSolution.java @@ -0,0 +1,35 @@ +package week11.sunghee.p1; + +import java.util.*; + +/** + * 0 패딩 -> exitTime으로 변경 + */ +public class RefactorSolution { + public int solution(int bridge_length, int weight, int[] truck_weights) { + Queue bridge = new ArrayDeque<>(); // [트럭무게, 다리에서 내려야하는 시간] + int time = 0; + int idx = 0; + int totalWeight = 0; + + while (idx < truck_weights.length || !bridge.isEmpty()) { + time++; + // + if (!bridge.isEmpty() && bridge.peek()[1] == time) { + totalWeight -= bridge.poll()[0]; + } + + // 2) 다음 트럭 진입 가능 여부 체크 + if (idx < truck_weights.length) { + int nextTruck = truck_weights[idx]; + if (totalWeight + nextTruck <= weight && bridge.size() < bridge_length) { + bridge.add(new int[]{nextTruck, time + bridge_length}); + totalWeight += nextTruck; + idx++; + } + } + } + + return time; + } +} diff --git a/src/week11/sunghee/p1/Solution.java b/src/week11/sunghee/p1/Solution.java new file mode 100644 index 0000000..e38642d --- /dev/null +++ b/src/week11/sunghee/p1/Solution.java @@ -0,0 +1,48 @@ +package week11.sunghee.p1; + + +import java.util.*; + +class Solution { + public int solution(int bridge_length, int weight, int[] truck_weights) { + //1. 조건에 안맞으면 크기가 0인 값을 넣기 + Queue bridge = new LinkedList<>(); + for(int i = 0; i < bridge_length; i++) { + bridge.add(0); + } + int bridgeWeight = 0; + int truckCont = 0; + int time = 0; + int idx = 0; + + + while(idx < truck_weights.length) { + //1. birdge pull + int outWeight = bridge.poll(); + time++; + if (outWeight > 0) { + bridgeWeight -= outWeight; + truckCont--; + } + + int curTruckWeight = truck_weights[idx]; + if(truckCont + 1 <= bridge_length && bridgeWeight + curTruckWeight <= weight) { + //조건 만족하면 트럭 add + bridge.add(curTruckWeight); + bridgeWeight += curTruckWeight; + truckCont++; + idx++; + } else { + //아니면 빈 stack 채우기 + bridge.add(0); + } + } + + while(!bridge.isEmpty()) { + int truck = bridge.poll(); + time++; + } + + return time; + } +} diff --git a/src/week11/sunghee/p2/Solution.java b/src/week11/sunghee/p2/Solution.java new file mode 100644 index 0000000..03af804 --- /dev/null +++ b/src/week11/sunghee/p2/Solution.java @@ -0,0 +1,66 @@ +package week11.sunghee.p2; + +import java.util.*; + + +public class Solution { + public int solution(int m, int n, String[] board) { + int answer = 0; + char[][] table = new char[m][n]; + for(int i=0; i