Count Unhappy Friends
Problem statement
There are n people. preferences[x] lists every other person from most to least preferred, and pairs assigns everyone one partner.
Person x is unhappy if there exists u whom x prefers over x's partner y, and u prefers x over u's partner v. Return the number of unhappy people.
Function
unhappyFriends(n: int, preferences: int[][], pairs: int[][]) → intExamples
Example 1
n = 4preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]]pairs = [[0,1],[2,3]]return = 2People 1 and 3 satisfy the reciprocal preference condition.
Constraints
2 <= n <= 500and n is even.- Each preference row is a permutation of all other people.
- Pairs form a perfect matching.