You are given an integer n, representing the number of friends labeled from preferences, where preferences[i] is a list of all other friends sorted in order of preference, from most preferred to least preferred.
The friends are divided into pairs represented by a pairs, where pairs[i] = [x_i, y_i] indicates that friend x_i is paired with friend y_i. Each friend belongs to exactly one pair.
A friend x is unhappy if x is paired with y, and there exists another friend u who is paired with v such that both of the following conditions hold:
x prefers u over y
u prefers x over v
Return the total number of unhappy friends.
Constraints:
n
n is even.
preferences.length
preferences[i].length
preferences[i][j]
preferences[i] does not contain i.
All values in preferences[i] are unique.
pairs.length
pairs[i].length
x_i y_i
x_i, y_i
Each person is contained in exactly one pair.
You are given an integer n, representing the number of friends labeled from preferences, where preferences[i] is a list of all other friends sorted in order of preference, from most preferred to least preferred.
The friends are divided into pairs represented by a pairs, where pairs[i] = [x_i, y_i] indicates that friend x_i is paired with friend y_i. Each friend belongs to exactly one pair.
A friend x is unhappy if x is paired with y, and there exists another friend u who is paired with v such that both of the following conditions hold:
x prefers u over y
u prefers x over v
Return the total number of unhappy friends.
Constraints:
n
n is even.
preferences.length
preferences[i].length
preferences[i][j]
preferences[i] does not contain i.
All values in preferences[i] are unique.
pairs.length
pairs[i].length
x_i y_i
x_i, y_i
Each person is contained in exactly one pair.