From 6520a7cab8fcfc5079cdd4fa9096a96804e02a0b Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:01:57 +0900 Subject: [PATCH 1/5] =?UTF-8?q?fix=20=EB=B0=B1=EB=8F=84=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\260\261\353\217\204\354\226\264.kt" | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git "a/src/main/kotlin/heejik/48week/\353\260\261\353\217\204\354\226\264.kt" "b/src/main/kotlin/heejik/48week/\353\260\261\353\217\204\354\226\264.kt" index 2839df4a..c657aa33 100644 --- "a/src/main/kotlin/heejik/48week/\353\260\261\353\217\204\354\226\264.kt" +++ "b/src/main/kotlin/heejik/48week/\353\260\261\353\217\204\354\226\264.kt" @@ -1,52 +1,48 @@ package heejik.`48week` -import java.io.BufferedReader -import java.io.InputStreamReader +import java.util.PriorityQueue import kotlin.properties.Delegates class 백도어 { var n by Delegates.notNull() var m by Delegates.notNull() - private lateinit var roads: MutableList> + private lateinit var roads: List> data class Road( val destination: Int, - val time: Int + val time: Long ) - fun solve() = with(BufferedReader(InputStreamReader(System.`in`))) { + fun solve() = with(System.`in`.bufferedReader()) { readLine().split(' ').map { it.toInt() }.run { n = this[0] m = this[1] } - val cant = mutableListOf() - readLine().split(' ').map { it.toInt() }.forEachIndexed { index, it -> - if (it == 1 && index != n - 1) cant.add(index) - } + val cant = readLine().split(' ').map { it == "1" } as MutableList + cant[n - 1] = false - roads = MutableList(n) { mutableListOf() } + roads = List(n) { mutableListOf() } repeat(m) { val (a, b, t) = readLine().split(' ').map { it.toInt() } - if (a in cant || b in cant) return@repeat - roads[a].add(Road(b, t)) - roads[b].add(Road(a, t)) + if (cant[a] || cant[b]) return@repeat // in 으로 해서 문제였구나,,, + roads[a].add(Road(b, t.toLong())) + roads[b].add(Road(a, t.toLong())) } - println(getMinTime(0)) } - fun getMinTime(start: Int): Int { - val times = MutableList(n) { Int.MAX_VALUE } - times[start] = 0 + fun getMinTime(start: Int): Long { + val times = LongArray(n) { Long.MAX_VALUE } + times[start] = 0L - val deque = ArrayDeque() - deque.add(Road(start, 0)) + val pq = PriorityQueue { a1, a2 -> (a1.time - a2.time).toInt() } + pq.add(Road(start, 0L)) - while (deque.isNotEmpty()) { - val (current, time) = deque.removeFirst() + while (pq.isNotEmpty()) { + val (current, time) = pq.poll() if (times[current] < time) continue roads[current].forEach { @@ -54,14 +50,15 @@ class 백도어 { val newTime = time + it.time if (newTime < times[destination]) { times[destination] = newTime - deque.add(Road(destination, newTime)) + pq.add(Road(destination, newTime)) } } } - return if (times[n - 1] == Int.MAX_VALUE) -1 else times[n - 1] + return if (times[n - 1] == Long.MAX_VALUE) -1 else times[n - 1] } } fun main() { 백도어().solve() -} \ No newline at end of file +} + From 31579f05f084a498aac1028116d71d6ea7fd3e61 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:02:20 +0900 Subject: [PATCH 2/5] =?UTF-8?q?solve=20=EB=85=B8=EB=93=9C=EC=82=AC?= =?UTF-8?q?=EC=9D=B4=EC=9D=98=20=EA=B1=B0=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \352\261\260\353\246\254.kt" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "src/main/kotlin/heejik/49week/\353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.kt" diff --git "a/src/main/kotlin/heejik/49week/\353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.kt" "b/src/main/kotlin/heejik/49week/\353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.kt" new file mode 100644 index 00000000..2ebcb783 --- /dev/null +++ "b/src/main/kotlin/heejik/49week/\353\205\270\353\223\234\354\202\254\354\235\264\354\235\230 \352\261\260\353\246\254.kt" @@ -0,0 +1,60 @@ +package heejik.`49week` + +import kotlin.properties.Delegates + +class `노드사이의 거리` { + + + private var n by Delegates.notNull() + private var m by Delegates.notNull() + lateinit var nodes: MutableList>> + + + fun solve() { + setting() + repeat(m) { + val (node1, node2) = readln().split(' ').map { it.toInt() } + bfs(firstStart = node1 - 1, destination = node2 - 1) + } + } + + private fun setting() { + readln().split(' ').map { it.toInt() }.run { + n = this[0] + m = this[1] + } + nodes = MutableList(n) { mutableListOf() } + repeat(n - 1) { + val (node1, node2, distance) = readln().split(' ').map { it.toInt() } + nodes[node1 - 1].add(node2 - 1 to distance) + nodes[node2 - 1].add(node1 - 1 to distance) + } + } + + private fun bfs(firstStart: Int, destination: Int) { + val deque = ArrayDeque>() + val visited = BooleanArray(size = n) + visited[firstStart] = true + deque.add(firstStart to 0) + + while (deque.isNotEmpty()) { + val (start, distance) = deque.removeFirst() + + if (start == destination) { + println(distance) + return + } + + for (node in nodes[start]) { + if (visited[node.first].not()) { + deque.add(node.first to distance + node.second) + visited[node.first] = true + } + } + } + } +} + +fun main() { + `노드사이의 거리`().solve() +} \ No newline at end of file From 5de4ef614767e23d9ca4c191ffca9642c4dadbfc Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:02:28 +0900 Subject: [PATCH 3/5] =?UTF-8?q?solve=20=EB=B2=BC=EB=9D=BD=EC=B9=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...74\353\235\275\354\271\230\352\270\260.kt" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "src/main/kotlin/heejik/49week/\353\262\274\353\235\275\354\271\230\352\270\260.kt" diff --git "a/src/main/kotlin/heejik/49week/\353\262\274\353\235\275\354\271\230\352\270\260.kt" "b/src/main/kotlin/heejik/49week/\353\262\274\353\235\275\354\271\230\352\270\260.kt" new file mode 100644 index 00000000..6c86a8ce --- /dev/null +++ "b/src/main/kotlin/heejik/49week/\353\262\274\353\235\275\354\271\230\352\270\260.kt" @@ -0,0 +1,40 @@ +package heejik.`49week` + +import kotlin.math.max + + +class 벼락치기 { + fun solve() { + val units = mutableListOf>() + + val (n, t) = readln().split(' ').map { it.toInt() } + + repeat(n) { + val (k, s) = readln().split(' ').map { it.toInt() } + units.add(k to s) + } + + val dp = MutableList(size = t + 1) { 0 } + val stored = MutableList(size = t + 1) { 0 } + + first@ for (unit in units) { + second@ for (time in 0..t) { + if (time + unit.first > t) break@second + stored[time + unit.first] = max(dp[time + unit.first], dp[time] + unit.second) + } + for (time in 0..t) { + if (stored[time] != 0) { + dp[time] = stored[time] + } + } + stored.fill(0) + } + + println(dp.last()) + } +} + + +fun main() { + 벼락치기().solve() +} \ No newline at end of file From 862604b4e109da175fc81edf63cd0182974c80b9 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:02:34 +0900 Subject: [PATCH 4/5] =?UTF-8?q?solve=20=EB=B9=97=EB=AC=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../49week/\353\271\227\353\254\274.kt" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "src/main/kotlin/heejik/49week/\353\271\227\353\254\274.kt" diff --git "a/src/main/kotlin/heejik/49week/\353\271\227\353\254\274.kt" "b/src/main/kotlin/heejik/49week/\353\271\227\353\254\274.kt" new file mode 100644 index 00000000..6f47f9ae --- /dev/null +++ "b/src/main/kotlin/heejik/49week/\353\271\227\353\254\274.kt" @@ -0,0 +1,51 @@ +package heejik.`49week` + +import kotlin.math.min +import kotlin.properties.Delegates + +class 빗물 { + + var h by Delegates.notNull() + var w by Delegates.notNull() + private lateinit var heights: List + + fun solve() { + setting() + getRainWater().also { rainWater -> + print(rainWater) + } + } + + private fun setting() { + readln().split(' ').map { it.toInt() }.run { + h = this[0] + w = this[1] + } + heights = readln().split(' ').map { it.toInt() } + } + + private fun getRainWater(): Int { + var rainWater = 0 + + heights.forEachIndexed { index, height -> + if (index == 0 || index == heights.size - 1) return@forEachIndexed + + val minHeightOfBothSideMaxHeight = min( + heights.subList(0, index + 1).max() ?: 0, + heights.subList(index + 1, heights.size).max() ?: 0 + ) + + with(minHeightOfBothSideMaxHeight - height) { + if (this > 0) rainWater += this + } + + } + + return rainWater + } +} + + +fun main() { + 빗물().solve() +} \ No newline at end of file From d2c396c50c1b80d0040acd8059c64c81c42dd7d0 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:03:04 +0900 Subject: [PATCH 5/5] =?UTF-8?q?solve=20=ED=95=A0=EC=9D=B8=20=ED=96=89?= =?UTF-8?q?=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\235\270 \355\226\211\354\202\254.kt" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "src/main/kotlin/heejik/49week/\355\225\240\354\235\270 \355\226\211\354\202\254.kt" diff --git "a/src/main/kotlin/heejik/49week/\355\225\240\354\235\270 \355\226\211\354\202\254.kt" "b/src/main/kotlin/heejik/49week/\355\225\240\354\235\270 \355\226\211\354\202\254.kt" new file mode 100644 index 00000000..4ccedbef --- /dev/null +++ "b/src/main/kotlin/heejik/49week/\355\225\240\354\235\270 \355\226\211\354\202\254.kt" @@ -0,0 +1,33 @@ +package heejik.`49week` + +data class Goods( + val name: String, + val count: Int +) + +class Solution { + fun solution(want: Array, number: IntArray, discount: Array): Int { + var answer = 0 + val shoppingBasket = mutableListOf() + val wantGoods = mutableListOf() + val totalCount = number.sum() + + want.forEachIndexed { index, goods -> + wantGoods.add(Goods(name = goods, count = number[index])) + } + + discount.forEach first@{ goods -> + if (shoppingBasket.size == totalCount) { + shoppingBasket.removeFirst() + } + shoppingBasket.add(goods) + + wantGoods.forEach { goods -> + if (shoppingBasket.count { it == goods.name } != goods.count) return@first + } + answer++ + } + + return answer + } +}