From 2518cf4d673dde77530e8a993489eb1dd0a87e4c Mon Sep 17 00:00:00 2001 From: mordiumaco Date: Mon, 27 Jul 2020 01:59:37 +0900 Subject: [PATCH 1/2] =?UTF-8?q?2-5=20=EB=8B=A4=EB=A6=AC=EB=A5=BC=20?= =?UTF-8?q?=EC=A7=80=EB=82=98=EB=8A=94=20=ED=8A=B8=EB=9F=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 7 --- .../Solution2_5.java" | 59 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) delete mode 100644 "week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution.java" create mode 100644 "week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution2_5.java" diff --git "a/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution.java" "b/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution.java" deleted file mode 100644 index 9c18d9a..0000000 --- "a/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution.java" +++ /dev/null @@ -1,7 +0,0 @@ -/* - 5. 다리를 지나는 트럭 - https://programmers.co.kr/learn/courses/30/lessons/42583 -*/ -public class Solution { - -} \ No newline at end of file diff --git "a/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution2_5.java" "b/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution2_5.java" new file mode 100644 index 0000000..a996d22 --- /dev/null +++ "b/week2/5. \353\213\244\353\246\254\353\245\274 \354\247\200\353\202\230\353\212\224 \355\212\270\353\237\255/Solution2_5.java" @@ -0,0 +1,59 @@ +import java.util.*; + +/* + 5. 다리를 지나는 트럭 + https://programmers.co.kr/learn/courses/30/lessons/42583 +*/ +public class Solution2_5 { + + public static void main(String[] args) { + + int bridge_weight = 100; + int bridge_length = 100; + int[] truck_weights = { 10 }; + + System.out.println(solution(bridge_length, bridge_weight, truck_weights)); + } + + public static int solution(int bridge_length, int weight, int[] truck_weights) { + Queue waiting = new LinkedList<>(); + Queue bridge = new LinkedList<>(); + + for (int i = 0; i < truck_weights.length; ++i) { + waiting.offer(new Truck(truck_weights[i], 0)); + } + + int time = 0; + int totalWeight = 0; + while (!waiting.isEmpty() || !bridge.isEmpty()) { + time++; + if (!bridge.isEmpty()) { + Truck t = bridge.peek(); + if (time - t.entry >= bridge_length) { + totalWeight -= t.weight; + bridge.poll(); + } + } + + if (!waiting.isEmpty()) { + if (totalWeight + waiting.peek().weight <= weight) { + Truck t = waiting.poll(); + totalWeight += t.weight; + + bridge.offer(new Truck(t.weight, time)); + } + } + } + return time; + } +} + +class Truck { + int weight; + int entry; + + Truck(int weight, int entry) { + this.weight = weight; + this.entry = entry; + } +} \ No newline at end of file From d99d657180bdc56497e8ae2329cc7192497a34b4 Mon Sep 17 00:00:00 2001 From: mordiumaco Date: Fri, 31 Jul 2020 02:55:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?2-6=20=EC=9C=84=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Solution.java" | 33 +++++++++++++++- .../Solution.java" | 7 ---- .../Solution2_6.java" | 38 +++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) delete mode 100644 "week2/6. \354\234\204\354\236\245/Solution.java" create mode 100644 "week2/6. \354\234\204\354\236\245/Solution2_6.java" diff --git "a/week2/2. \354\230\254\353\260\224\353\245\270 \352\264\204\355\230\270/Solution.java" "b/week2/2. \354\230\254\353\260\224\353\245\270 \352\264\204\355\230\270/Solution.java" index 2150163..f11caa7 100644 --- "a/week2/2. \354\230\254\353\260\224\353\245\270 \352\264\204\355\230\270/Solution.java" +++ "b/week2/2. \354\230\254\353\260\224\353\245\270 \352\264\204\355\230\270/Solution.java" @@ -2,6 +2,37 @@ 2. 올바른 괄호 https://programmers.co.kr/learn/courses/30/lessons/12909 */ -public class Solution { +import java.util.HashMap; +import java.util.Map; +public class Solution2_6 { + public static void main(String[] args) { + String[][] clothes = { { "yellow_hat", "headgear" }, { "blue_sunglasses", "eyewear" }, + { "green_turban", "headgear" } }; + + System.out.println(solution(clothes)); + } + + public static int solution(String[][] clothes) { + int answer = 1; + + Map cloteshMap = new HashMap<>(); + + for (String[] cloth : clothes) { + + cloteshMap.put(cloth[1], cloteshMap.getOrDefault(cloth[1], 0) + 1); + + } + + for (Map.Entry cloth : cloteshMap.entrySet()) { + + answer *= (cloth.getValue() + 1); + + } + + answer--; + + return answer; + + } } \ No newline at end of file diff --git "a/week2/6. \354\234\204\354\236\245/Solution.java" "b/week2/6. \354\234\204\354\236\245/Solution.java" deleted file mode 100644 index 7361a04..0000000 --- "a/week2/6. \354\234\204\354\236\245/Solution.java" +++ /dev/null @@ -1,7 +0,0 @@ -/* - 7. 전화번호 목록 - https://programmers.co.kr/learn/courses/30/lessons/42577 -*/ -public class Solution { - -} \ No newline at end of file diff --git "a/week2/6. \354\234\204\354\236\245/Solution2_6.java" "b/week2/6. \354\234\204\354\236\245/Solution2_6.java" new file mode 100644 index 0000000..80d5156 --- /dev/null +++ "b/week2/6. \354\234\204\354\236\245/Solution2_6.java" @@ -0,0 +1,38 @@ +/* + 6. 위장 + https://programmers.co.kr/learn/courses/30/lessons/42578 +*/ +import java.util.HashMap; +import java.util.Map; + +public class Solution2_6 { + public static void main(String[] args) { + String[][] clothes = { { "yellow_hat", "headgear" }, { "blue_sunglasses", "eyewear" }, + { "green_turban", "headgear" } }; + + System.out.println(solution(clothes)); + } + + public static int solution(String[][] clothes) { + int answer = 1; + + Map cloteshMap = new HashMap<>(); + + for (String[] cloth : clothes) { + + cloteshMap.put(cloth[1], cloteshMap.getOrDefault(cloth[1], 0) + 1); + + } + + for (Map.Entry cloth : cloteshMap.entrySet()) { + + answer *= (cloth.getValue() + 1); + + } + + answer--; + + return answer; + + } +} \ No newline at end of file