Skip to content

Commit

Permalink
Create 1376-time-needed-to-inform-all-employees.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Jun 3, 2023
1 parent a9518fb commit 57e09db
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 1376-time-needed-to-inform-all-employees.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
fun numOfMinutes(n: Int, headID: Int, manager: IntArray, informTime: IntArray): Int {
val adj = HashMap<Int, ArrayList<Int>>().apply {
for (i in 0 until n)
this[manager[i]] = getOrDefault(manager[i], ArrayList<Int>()).apply { add(i) }
}

var res = 0
with (LinkedList<Pair<Int, Int>>()){
addLast(headID to 0)
while(isNotEmpty()) {
val (id, time) = removeFirst()
res = maxOf(res, time)
adj[id]?.forEach {
addLast(it to (time + informTime[id]))
}
}
}

return res
}
}

0 comments on commit 57e09db

Please sign in to comment.