Max Level Party Invites
You are given a list of n employees each assigned a level between 0 and 10. The employees and their levels are represented as an array of integers, levels, where the index, i, is the employee's identifier and the value levels[i] is their level.
You are also provided a reporting_chain which represents the reporting structure of employees where each entries first value is the manager and the second value in that entry is their direct report.
For example, a reporting_chain that consists of [[0, 1], [0, 2], [1, 3]] would indicate:
Employee 0 manages Employee 1 Employee 0 manages Employee 2 Employee 1 manages Employee 3
You are tasked with inviting people from this company to a party, however, the constraint is the if an employee is invited, none of their direct reports can be invited.
Following this constraint, return the max sum of levels possible given a selection of employees to invite.
Example 1:
Example 2:
Example 3:
Notes
Intuition
This is a tree DP problem similar to "House Robber III." For each employee, we compute two values: the maximum sum if we invite them (must skip their direct reports) and if we skip them (can choose either for reports).
Implementation
Build an adjacency list from the reporting chain and identify root employees (those without managers). DFS with memoization computes (invite, skip) pairs bottom-up. If we invite an employee, add their level plus the skip values of all reports. If we skip, add max(invite, skip) of each report. Process all roots and sum the maximum of invite/skip for each tree.
Edge-cases
The graph may be a forest (multiple disconnected trees). Track which nodes have parents to identify all roots and process each independently.
- Time: O(n) — each node processed exactly once via memoization
- Space: O(n) — adjacency list and memoization cache