Finish Time of Tasks II Problem
Description
LeetCode Problem 3967.
You are given an integer n representing the number of tasks in a project, numbered from 0 to n - 1. These tasks are connected as an undirected tree. This is represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates an undirected connection between task u_i and task v_i.
You are also given an array baseTime of length n, where baseTime[i] represents the time to complete task i.
For any chosen task as the root, the finish time of each task is calculated as follows:
- Leaf task: The finish time is
baseTime[i]. - Non-leaf task:
- Let
earliestbe the minimum finish time among its children, andlatestbe the maximum finish time among its children. - Let
ownDurationbe(latest - earliest) + baseTime[i]. - Finish time of task
iislatest + ownDuration.
- Let
Choose any task as the root and compute the finish time of that root based on the rules above.
Return the minimum possible finish time among all choices of root.
Example 1:
Input: n = 3, edges = [[0,1],[1,2]], baseTime = [9,1,5]
Output: 14
Explanation:
The optimal choice is to treat task 1 as the root.
- Task 0 is a leaf, so its finish time is baseTime[0] = 9.
- Task 2 is a leaf, so its finish time is baseTime[2] = 5.
- Task 1 has two children with finish times 9 and 5:
- earliest = 5, latest = 9
- ownDuration = (latest - earliest) + baseTime[1] = (9 - 5) + 1 = 5
- Finish time of task 1 is latest + ownDuration = 9 + 5 = 14
Thus, the minimum possible finish time among all choices of root is 14.
Example 2:
Input: n = 3, edges = [[0,1],[0,2]], baseTime = [4,7,6]
Output: 12
Explanation:
The optimal choice is to treat task 0 as the root.
- Task 1 is a leaf, so its finish time is baseTime[1] = 7.
- Task 2 is a leaf, so its finish time is baseTime[2] = 6.
- Task 0 has two children with finish times 7 and 6:
- earliest = 6, latest = 7
- ownDuration = (latest - earliest) + baseTime[0] = (7 - 6) + 4 = 5
- Finish time of task 0 is latest + ownDuration = 7 + 5 = 12
Thus, the minimum possible finish time among all choices of root is 12.
Example 3:
Input: n = 4, edges = [[0,1],[0,2],[2,3]], baseTime = [5,8,2,1]
Output: 16
Explanation:
The optimal choice is to treat task 1 as the root.
- Task 3 is a leaf, so its finish time is baseTime[3] = 1.
- Task 2 has one child task 3:
- earliest = latest = 1
- ownDuration = (latest - earliest) + baseTime[2] = 0 + 2 = 2
- Finish time of task 2 is latest + ownDuration = 1 + 2 = 3
- Task 0 has one child task 2:
- earliest = latest = 3
- ownDuration = (latest - earliest) + baseTime[0] = 0 + 5 = 5
- Finish time of task 0 is latest + ownDuration = 3 + 5 = 8
- Task 1 has one child task 0:
- earliest = latest = 8
- ownDuration = (latest - earliest) + baseTime[1] = 0 + 8 = 8
- Finish time of task 1 is latest + ownDuration = 8 + 8 = 16
Thus, the minimum possible finish time among all choices of root is 16.
Constraints:
1 <= n <= 10^5edges.length = n - 1edges[i] == [u_i, v_i]0 <= u_i, v_i <= n - 1u_i != v_i- The input is generated such that
edgesrepresents a valid undirected tree. baseTime.length == n1 <= baseTime[i] <= 10^5
Solution
Approach: Rerooting
Part I computed the finish time of the root with one DFS. Here every task may be the root, and running that DFS n times is too slow, so we reuse the work with rerooting.
The finish time of a task depends only on its base time and on the finish times of its children, that is, of all its neighbors except its parent. Root the tree at task 0 and define two values for each task v:
down[v]: the finish time ofvwhen it keeps only its children. Compute these from the leaves up.up[c]: for a childcofv, the finish time ofvwhencis its parent, sovkeeps all its neighbors exceptc.
Going from the root down, task v knows the finish times of all its neighbors: down[u] for each child u, and up[v] for its parent. From their minimum and maximum we get the finish time of v as the root of the whole tree, which is a candidate for the answer.
For up[c] we need the minimum and maximum over the neighbors of v without c. Keep the two smallest and the two largest neighbor values: if down[c] is the smallest, use the second smallest, otherwise use the smallest (the same for the maximum). A task with no remaining neighbors is a leaf, so its finish time is its base time.
The answer is the smallest candidate over all tasks. All traversals use a BFS order, so there is no deep recursion.
Complexity: O(n) time and O(n) space.
from math import inf
class Solution:
def finishTime(self, n: int, edges: List[List[int]], baseTime: List[int]) -> int:
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
# finish time of a task that has `cnt` children whose finish times lie in [earliest, latest]
def finish(i: int, cnt: int, earliest: int, latest: int) -> int:
if cnt == 0:
return baseTime[i]
return latest + (latest - earliest) + baseTime[i]
# BFS order from task 0, so parents come before their children
parent = [-1] * n
order = [0]
for v in order:
for u in g[v]:
if u != parent[v]:
parent[u] = v
order.append(u)
# down[v]: finish time of v when its parent is the root side (v keeps only its children)
down = [0] * n
for v in reversed(order):
earliest, latest, cnt = inf, -inf, 0
for u in g[v]:
if u != parent[v]:
earliest = min(earliest, down[u])
latest = max(latest, down[u])
cnt += 1
down[v] = finish(v, cnt, earliest, latest)
# up[v]: finish time of parent[v] when v is its parent (parent[v] keeps all neighbors except v)
up = [0] * n
ans = inf
for v in order:
vals = [down[u] if u != parent[v] else up[v] for u in g[v]]
lo = sorted(vals)[:2] + [inf, inf]
hi = sorted(vals, reverse=True)[:2] + [-inf, -inf]
ans = min(ans, finish(v, len(vals), lo[0], hi[0]))
for u in g[v]:
if u == parent[v]:
continue
earliest = lo[1] if down[u] == lo[0] else lo[0]
latest = hi[1] if down[u] == hi[0] else hi[0]
up[u] = finish(v, len(vals) - 1, earliest, latest)
return ans