Team-Preferred Stable Matching
Problem statement
There are n teams and n new employees. Every team has one position, and every employee joins exactly one team.
teamPreferences[t]lists all employee indices from most to least preferred by teamt.employeePreferences[e]lists all team indices from most to least preferred by employeee.
Return the team-optimal stable matching. In other words, among stable matchings, teams receive priority: run the team-proposing Gale-Shapley process. Return an array assignment where assignment[t] is the employee matched to team t.
A matching is stable when there is no unmatched pair (t, e) such that team t prefers employee e over its assigned employee and employee e prefers team t over their assigned team.
Function
teamPreferredMatching(teamPreferences: int[][], employeePreferences: int[][]) → int[]Examples
Example 1
teamPreferences = [[0,1],[0,1]]employeePreferences = [[0,1],[1,0]]return = [0,1]Both teams first propose to employee 0. Employee 0 keeps team 0, so team 1 next proposes to employee 1.
Example 2
teamPreferences = [[0,1,2],[1,0,2],[1,2,0]]employeePreferences = [[1,0,2],[0,2,1],[2,1,0]]return = [1,0,2]Rejections cascade: employee 1 upgrades from team 1 to team 2 and then to team 0; employee 0 upgrades from team 0 to team 1, leaving team 2 with employee 2.
Example 3
teamPreferences = [[0]]employeePreferences = [[0]]return = [0]The only team and employee must be matched.
Constraints
1 <= n == teamPreferences.length == employeePreferences.length <= 1000.- Every row in both matrices has length
n. - Every team-preference row is a permutation of employee indices
0..n-1. - Every employee-preference row is a permutation of team indices
0..n-1.