Topmost Accessible Nodes
Problem statement
A hierarchy contains teams, folders, and files. It is represented as a forest of nodes.
nodeIds[i]is the unique ID of nodei.parent[i]is the parent index, or-1when nodeiis a root.readableUsers[i]lists the users with direct read access to nodei.
Direct access to a node also grants access to every descendant. For userId, return the smallest covering set of nodes: whenever the user has direct access to a node, include that node and do not include any accessible descendant beneath it.
Return selected node IDs in forest preorder. Roots and siblings follow their order in the input arrays.
Function
fewestAccessibleNodes(nodeIds: String[], parent: int[], readableUsers: String[][], userId: String) → String[]Examples
Example 1
nodeIds = ["Team1","Folder1","File1","File2","Folder2","Folder3"]parent = [-1,0,1,1,0,4]readableUsers = [[],["A"],["A"],[],[],["A"]]userId = "A"return = ["Folder1","Folder3"]Folder1 covers both files beneath it, so File1 is redundant. Folder3 is in another branch and is also required.
Example 2
nodeIds = ["Team1","Folder1","File1","Folder2"]parent = [-1,0,1,0]readableUsers = [["A"],["A"],["A"],[]]userId = "A"return = ["Team1"]Access at the root covers every descendant, so it is the only selected node.
Example 3
nodeIds = ["RootA","LeafA","RootB","LeafB"]parent = [-1,0,-1,2]readableUsers = [[],["U"],[],["U"]]userId = "U"return = ["LeafA","LeafB"]Neither root grants access, so each directly readable leaf must be returned.
Constraints
1 <= nodeIds.length <= 200000.nodeIds.length == parent.length == readableUsers.length.- Node IDs are unique.
- For every non-root node
i,0 <= parent[i] < i. - The total number of direct user entries is at most
200000.