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

[장희직] - 트리의 지름, 공유기 설치 #249

Merged
merged 4 commits into from
Apr 29, 2024
Merged

Conversation

jhg3410
Copy link
Member

@jhg3410 jhg3410 commented Apr 29, 2024

📌 from issue #248 📌

📋문제 목록📋

트리의 지름: ⛔️
공유기 설치: ✅
뮤탈리스크:  ❎

📍추가로 해결한 문제📍

스티커 재배치: 👍  

@jhg3410 jhg3410 added the 희직 label Apr 29, 2024
@jhg3410 jhg3410 self-assigned this Apr 29, 2024
Comment on lines +52 to +81
if (liveCount == 1) {
queue.add(
nowHps.first.copy(
hp1 = nowHps.first.hp1 - 9,
hp2 = nowHps.first.hp2 - 9,
hp3 = nowHps.first.hp3 - 9,
) to nowHps.second + 1
)
} else if (liveCount == 2) {
val (hp1, hp2) = nowHps.first.getLiveHP()
for ((case1, case2) in listOf((9 to 3), (3 to 9))) {
queue.add(
nowHps.first.copy(
hp1 = hp1 - case1,
hp2 = hp2 - case2,
hp3 = 0
) to nowHps.second + 1
)
}
} else {
for (case in cases) {
queue.add(
nowHps.first.copy(
hp1 = nowHps.first.hp1 - case[0],
hp2 = nowHps.first.hp2 - case[1],
hp3 = nowHps.first.hp3 - case[2],
) to nowHps.second + 1
)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분 코드가 예술로 예쁘다고 생각했슴다^^

Comment on lines +29 to +36
while (start <= end) {
val middle = (start + end) / 2
if (canInstall(middle)) { // 설치가 가능하면 오른쪽을 본다.
start = middle + 1
} else { // 설치가 안되면 왼쪽을 본다.
end = middle - 1
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

직접 다 계산해서 이렇게 깔끔하게 나오도록 짜시다니 멋지네요


import kotlin.math.min

class `스티커 재배치` {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 좋은 문제 공유 감사합니다

nodes[child].add(root to distance)
}

fun bfs(start: Int): Pair<Int, Int> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아예 그래프 탐색처럼도 풀 수 있었군여

Comment on lines +19 to +43
fun bfs(start: Int): Pair<Int, Int> {
visited.fill(element = false)
// 원소, 가중치
queue.add(start to 0)
visited[start] = true
var maxDis = 0
var maxNumber = 0

while (queue.isNotEmpty()) {
val (number, sumDistance) = queue.removeFirst()
nodes[number].filter { visited[it.first].not() }.forEach {
queue.add(it.first to sumDistance + it.second)
visited[it.first] = true
}
if (sumDistance > maxDis) {
maxDis = sumDistance
maxNumber = number
}
}

return maxNumber to maxDis
}

val (number, _) = bfs(1)
println(bfs(number).second)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 새롭게 알아갑니다!!! 👍


import kotlin.math.min

class `스티커 재배치` {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 추천 감사합니다~~

Comment on lines +26 to +36
var start = 1L
var end: Long = homes.last() - homes.first()

while (start <= end) {
val middle = (start + end) / 2
if (canInstall(middle)) { // 설치가 가능하면 오른쪽을 본다.
start = middle + 1
} else { // 설치가 안되면 왼쪽을 본다.
end = middle - 1
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 방식은 end에 1을 붙이지 않아도 되는군요 ~~

@jhg3410 jhg3410 merged commit cf4250f into main Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants