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

[장희직] - 노드사이의 거리, 빗물, 벼락치기, 할인 행사 #199

Merged
merged 5 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
45 changes: 21 additions & 24 deletions src/main/kotlin/heejik/48week/백도어.kt
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
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<Int>()
var m by Delegates.notNull<Int>()
private lateinit var roads: MutableList<MutableList<Road>>
private lateinit var roads: List<MutableList<Road>>

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<Int>()
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<Road>()
deque.add(Road(start, 0))
val pq = PriorityQueue<Road> { 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 {
val destination = it.destination
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()
}
}

60 changes: 60 additions & 0 deletions src/main/kotlin/heejik/49week/노드사이의 거리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package heejik.`49week`

import kotlin.properties.Delegates

class `노드사이의 거리` {


private var n by Delegates.notNull<Int>()
private var m by Delegates.notNull<Int>()
lateinit var nodes: MutableList<MutableList<Pair<Int, Int>>>


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<Pair<Int, Int>>()
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()
}
40 changes: 40 additions & 0 deletions src/main/kotlin/heejik/49week/벼락치기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package heejik.`49week`

import kotlin.math.max


class 벼락치기 {
fun solve() {
val units = mutableListOf<Pair<Int, Int>>()

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()
}
51 changes: 51 additions & 0 deletions src/main/kotlin/heejik/49week/빗물.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package heejik.`49week`

import kotlin.math.min
import kotlin.properties.Delegates

class 빗물 {

var h by Delegates.notNull<Int>()
var w by Delegates.notNull<Int>()
private lateinit var heights: List<Int>

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()
}
33 changes: 33 additions & 0 deletions src/main/kotlin/heejik/49week/할인 행사.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package heejik.`49week`

data class Goods(
val name: String,
val count: Int
)

class Solution {
fun solution(want: Array<String>, number: IntArray, discount: Array<String>): Int {
var answer = 0
val shoppingBasket = mutableListOf<String>()
val wantGoods = mutableListOf<Goods>()
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
}
}