Skip to content

Commit

Permalink
Merge pull request #198 from wellFoundedDevelopers/hyunsoo/49week
Browse files Browse the repository at this point in the history
[전현수] - 노드사이의 거리, 빗물, 벼락치기, 할인 행사
  • Loading branch information
soopeach authored Nov 20, 2023
2 parents 3acd36c + 2e0432e commit 2bd6031
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 0 deletions.
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)
}
}

0 comments on commit 2bd6031

Please sign in to comment.