Skip to content

Commit

Permalink
Merge pull request #177 from AlgoLeadMe/18-jung0115
Browse files Browse the repository at this point in the history
18-jung0115
  • Loading branch information
jung0115 authored Nov 21, 2024
2 parents 848bbae + 5f9a779 commit 9713ecf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion jung0115/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
| 14차시 | 2024.09.25.수 | 그리디 알고리즘 | [컵라면(1781)](https://www.acmicpc.net/problem/1781) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/165 |
| 15차시 | 2024.09.28.토 | 이분 탐색 | [가장 긴 증가하는 부분 수열 2(12015)](https://www.acmicpc.net/problem/12015) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/169 |
| 16차시 | 2024.10.05.토 | 시뮬레이션 | [주차 요금 계산(Lv.2)](https://school.programmers.co.kr/learn/courses/30/lessons/92341) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/173 |
| 17차시 | 2024.10.11.금 | 그리디 알고리즘 | [마법의 엘리베이터(Lv.2)](https://school.programmers.co.kr/learn/courses/30/lessons/148653) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/175 |
| 17차시 | 2024.10.11.금 | 그리디 알고리즘 | [마법의 엘리베이터(Lv.2)](https://school.programmers.co.kr/learn/courses/30/lessons/148653) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/175 |
| 18차시 | 2024.10.12.토 | 다이나믹 프로그래밍 | [뉴스 전하기(1135)](https://www.acmicpc.net/problem/1135) | https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/177 |
46 changes: 46 additions & 0 deletions jung0115/다이나믹프로그래밍/Baekjoon_1135.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 18차시 2024.10.12.토 : 백준 - 뉴스 전하기(1135)
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.StringTokenizer

lateinit var employees: Array<MutableList<Int>>
lateinit var dp: Array<Int>

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))

var N = br.readLine().toInt() // 직원의 수

val st = StringTokenizer(br.readLine())
employees = Array(N) { mutableListOf<Int>() }
dp = Array<Int>(N) { -1 }

st.nextToken()
for(i: Int in 1..N-1) {
employees[st.nextToken().toInt()].add(i)
}

println(dfs(0))
}

fun dfs(employee: Int): Int {
if (dp[employee] != -1) return dp[employee]

// 더 이상 전화할 사람이 없음
if (employees[employee].isEmpty()) return 0

// 자식들에게 전화하는 시간
// 내림차순 정렬
val times = employees[employee].map { dfs(it) }.sortedDescending()

// 각 자식에게 전화 거는 시간 계산
var maxTime = 0
for (i in times.indices) {
// 자식에게 전화 + 그 자식이 전화 거는 시간
maxTime = maxOf(maxTime, times[i] + i + 1)
}

dp[employee] = maxTime

return dp[employee]
}

0 comments on commit 9713ecf

Please sign in to comment.