Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[전현수] - 노드사이의 거리, 빗물, 벼락치기, 할인 행사 #198

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/main/kotlin/hyunsoo/49week/노드사이의 거리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package hyunsoo.`49week`

import java.util.*

/**
*
* <문제>
* [노드사이의 거리](https://www.acmicpc.net/problem/1240)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_노드사이의_거리` {

private data class Bundle(val node: Int, val cost: Int)

private lateinit var treeInfo: Array<IntArray>

fun solution() {

val (nodeCnt, wantToKnowCnt) = readln().split(" ").map { it.toInt() }

treeInfo = Array(nodeCnt + 1) {
IntArray(nodeCnt + 1)
}

repeat(nodeCnt - 1) {
val (start, end, cost) = readln().split(" ").map { it.toInt() }
treeInfo[start][end] = cost
treeInfo[end][start] = cost
}

repeat(wantToKnowCnt) {

val (start, end) = readln().split(" ").map { it.toInt() }

val visited = BooleanArray(nodeCnt + 1)

val queue: Queue<Bundle> = LinkedList()

treeInfo[start].forEachIndexed { nodeIndex, cost ->

if (cost == NO_WAY) return@forEachIndexed

visited[nodeIndex] = true
queue.add(Bundle(nodeIndex, cost))

}

while (queue.isNotEmpty()) {

val (node, preCost) = queue.poll()

if (node == end) {
println(preCost)
return@repeat
}

treeInfo[node].forEachIndexed { nodeIndex, cost ->

if (cost == NO_WAY) return@forEachIndexed

if (visited[nodeIndex]) return@forEachIndexed

visited[nodeIndex] = true
queue.add(Bundle(nodeIndex, preCost + cost))

}

}

}
}

companion object {
const val NO_WAY = 0
}
}

fun main() {
전현수_노드사이의_거리().solution()
}
59 changes: 59 additions & 0 deletions src/main/kotlin/hyunsoo/49week/벼락치기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hyunsoo.`49week`

import kotlin.math.max

/**
*
* <문제>
* [벼락치기](https://www.acmicpc.net/problem/14728)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_벼락치기` {

private data class Question(val expectedTime: Int, val score: Int)

fun solution() {

val (unitCnt, totalTime) = readln().split(" ").map { it.toInt() }

val questionList = mutableListOf<Question>().apply {
add(Question(0, 0))
}

repeat(unitCnt) {

val (expectedTime, score) = readln().split(" ").map { it.toInt() }
questionList.add(Question(expectedTime, score))

}

val dp = Array(unitCnt + 1) {
IntArray(totalTime + 1)
}
for (i in 1..unitCnt) {
val curQuestion = questionList[i]
for (j in 0..totalTime) {
if (curQuestion.expectedTime <= j) {
dp[i][j] = max(
dp[i - 1][j],
dp[i - 1][j - curQuestion.expectedTime] + curQuestion.score
)
} else {
dp[i][j] = dp[i - 1][j]
}
}
}

println(dp[unitCnt][totalTime])


}
}

fun main() {
전현수_벼락치기().solution()
}
41 changes: 41 additions & 0 deletions src/main/kotlin/hyunsoo/49week/빗물.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hyunsoo.`49week`

import java.util.*
import kotlin.math.min

/**
*
* <문제>
* [빗물](https://www.acmicpc.net/problem/14719)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_빗물` {

fun solution() {

val (h, w) = readln().split(" ").map { it.toInt() }
val heightInfo = readln().split(" ").map { it.toInt() }
var amount = 0

for (i in 1 until w) {

val leftMax = heightInfo.subList(i, heightInfo.size).maxOf { it }
val rightMax = heightInfo.subList(0, i + 1).maxOf { it }

val minHeight = min(leftMax, rightMax)

if (heightInfo[i] < minHeight) amount += minHeight - heightInfo[i]
}


println(amount)
}
}

fun main() {
전현수_빗물().solution()
}
63 changes: 63 additions & 0 deletions src/main/kotlin/hyunsoo/49week/할인 행사.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package hyunsoo.`49week`

/**
*
* <문제>
* [할인 행사](https://school.programmers.co.kr/learn/courses/30/lessons/131127)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_할인_행사` {

fun solution(want: Array<String>, number: IntArray, discount: Array<String>): Int {

var answer = 0

for (i in 0 .. discount.size - 10) {

val wishList = mutableMapOf<String, Int>()

want.forEachIndexed { index, item ->
wishList[item] = number[index]
}

repeat(10) {
val index = i + it
wishList[discount[index]]?.let { pre ->
wishList[discount[index]] = pre - 1
}
}
if (wishList.values.all { it <= 0 }) answer += 1
}

return answer
}
}

fun main() {
전현수_할인_행사().solution(
arrayOf("banana", "apple", "rice", "pork", "pot"),
intArrayOf(3, 2, 2, 2, 1),
arrayOf(
"chicken",
"apple",
"apple",
"banana",
"rice",
"apple",
"pork",
"banana",
"pork",
"rice",
"pot",
"banana",
"apple",
"banana"
)
).apply {
println(this)
}
}