Problem · Tree

Employee Subordinates

Learn this problem
EasyInMobi logoInMobiNEW GRADOA

Problem statement

A company has n employees numbered from 1 through n. Employee 1 is the general director.

The array bosses has length n - 1. For each index i, bosses[i] is the direct boss of employee i + 2.

Return an array subordinateCounts of length n, where subordinateCounts[i] is the number of direct and indirect subordinates of employee i + 1.

Function

countSubordinates(bosses: int[]) → int[]

Examples

Example 1

bosses = [1,1,2,3]return = [4,1,1,0,0]

Employee 1 has all four other employees as subordinates. Employees 2 and 3 each have one subordinate, while employees 4 and 5 have none.

Example 2

bosses = [1,2,3,4]return = [4,3,2,1,0]

The hierarchy is one chain: employee 1 manages employee 2, who manages employee 3, and so on.

Example 3

bosses = []return = [0]

There is only the general director, so no employee has a subordinate.

Constraints

  • 1 <= bosses.length + 1 <= 2 * 10^5
  • 1 <= bosses[i] <= bosses.length + 1
  • The relationships in bosses form a valid rooted company hierarchy with employee 1 as the general director.

More InMobi problems

drafts saved locally
public int[] countSubordinates(int[] bosses) {
  // write your code here.
}
bosses[1,1,2,3]
expected[4,1,1,0,0]
checking account