Sort Departments by Enrollment
Learn this problemProblem statement
You are given parallel arrays departmentIds and departmentNames. Index i describes one department whose unique identifier is departmentIds[i] and whose unique name is departmentNames[i]. You are also given studentDepartmentIds, where each value is the department identifier referenced by one student.
Return one row [departmentId, studentCount] for every department, including departments with no students. Sort the rows by studentCount in descending order. When two counts are equal, compare their unique names in ascending, case-sensitive ASCII code-point order.
Function
sortDepartmentsByEnrollment(departmentIds: int[], departmentNames: String[], studentDepartmentIds: int[]) → int[][]Examples
Example 1
departmentIds = [10,20,30]departmentNames = ["Sales","Engineering","Design"]studentDepartmentIds = [20,20,10]return = [[20,2],[10,1],[30,0]]Engineering has two students, Sales has one, and Design has none.
Example 2
departmentIds = [3,1,2]departmentNames = ["Zoo","Alpha","Beta"]studentDepartmentIds = [3,2]return = [[2,1],[3,1],[1,0]]Beta and Zoo each have one student, so their tie is resolved alphabetically. Alpha has no students and appears last.
Example 3
departmentIds = [8,4]departmentNames = ["Physics","Art"]studentDepartmentIds = []return = [[4,0],[8,0]]Both counts are zero, so Art precedes Physics alphabetically.
Constraints
1 <= departmentIds.length == departmentNames.length <= 100000.0 <= studentDepartmentIds.length <= 200000.- Department identifiers are unique signed 32-bit integers.
- Department names are unique non-empty strings containing visible ASCII characters.
- Every value in
studentDepartmentIdsoccurs indepartmentIds. - Lexicographic comparison is case-sensitive.