Quick Overview

Given each friend's ranked preference list and a fixed pairing, count how many friends would rather be with someone who also prefers them over their own partner. It tests turning a two-sided preference condition into fast rank comparisons and counting each person only once.

Count Unhappy Friends in a Pairing Given Ranked Preference Lists

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of `preferences` for `n` friends, where `n` is always even. For each person `i`, `preferences[i]` contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from `0` to `n-1`. All the friends are divided into pairs. The pairings are given in a list `pairs`, where `pairs[i] = [x_i, y_i]` denotes that `x_i` is paired with `y_i` and `y_i` is paired with `x_i`. This pairing may leave some of the friends unhappy. A friend `x` is unhappy if `x` is paired with `y` and there exists a friend `u` who is paired with `v` such that: - `x` prefers `u` over `y`, and - `u` prefers `x` over `v`. Return the number of unhappy friends. ### Function Signature ```python def count_unhappy_friends(n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int: ``` ### Rules - `preferences[i]` lists every friend other than `i`, from most preferred to least preferred. - Count people, not reasons. A friend who is unhappy because of several different `u` still adds 1 to the answer. ### Constraints - `2 <= n <= 500` - `n` is even. - `preferences.length == n` - `preferences[i].length == n - 1` - `0 <= preferences[i][j] <= n - 1` - `preferences[i]` does not contain `i`. - All values in `preferences[i]` are unique. - `pairs.length == n / 2` - `pairs[i].length == 2` - `0 <= x_i, y_i <= n - 1` - Each person is contained in exactly one pair. ### Examples ```text Input: n = 4, preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]] Output: 2 ``` Friend 1 is unhappy because they prefer friend 3 over their partner 0, and friend 3 prefers friend 1 over their partner 2. For the same reason friend 3 is unhappy, while friends 0 and 2 are paired with their first choice and are happy. ```text Input: n = 2, preferences = [[1],[0]], pairs = [[1,0]] Output: 0 ``` Both friends are paired with their only choice, so no one is unhappy. ```text Input: n = 4, preferences = [[2,3,1],[0,2,3],[0,1,3],[0,1,2]], pairs = [[0,1],[2,3]] Output: 3 ``` Friend 0 prefers both 2 and 3 over partner 1, and both 2 and 3 rank 0 first, so friends 0, 2 and 3 are unhappy (0 is counted once). Friend 1 is paired with their first choice and is happy.

Overview: Given each friend's ranked preference list and a fixed pairing, count how many friends would rather be with someone who also prefers them over their own partner. It tests turning a two-sided preference condition into fast rank comparisons and counting each person only once.

There are `n` friends, labeled `0` to `n - 1`, where `n` is always even. For each friend `i`, `preferences[i]` lists every friend other than `i`, sorted from most preferred to least preferred. A friend who appears earlier in the list is preferred over a friend who appears later. All the friends are divided into pairs, given in the list `pairs`. Each `pairs[i] = [x_i, y_i]` means that `x_i` is paired with `y_i` and `y_i` is paired with `x_i`. Either member of a pair may be listed first. A friend `x` who is paired with `y` is unhappy if there exists a friend `u`, paired with `v`, such that both of the following hold: - `x` prefers `u` over `y`, and - `u` prefers `x` over `v`. Implement `count_unhappy_friends(n, preferences, pairs)` and return the number of unhappy friends. Each unhappy friend adds exactly 1 to the answer, even when several different choices of `u` make that friend unhappy. The answer is an integer from `0` to `n`, so it never exceeds 2^31 - 1 and a 32-bit `int` holds it in every language. ### Example 1 ```text Input: n = 4, preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]] Output: 2 ``` Friend 1 prefers friend 3 over partner 0, and friend 3 prefers friend 1 over partner 2, so friend 1 is unhappy. For the same reason friend 3 is unhappy. Friends 0 and 2 are paired with their first choice and are happy. ### Example 2 ```text Input: n = 4, preferences = [[2,3,1],[0,2,3],[0,1,3],[0,1,2]], pairs = [[0,1],[2,3]] Output: 3 ``` Friend 0 prefers both 2 and 3 over partner 1, and both 2 and 3 rank 0 first, so friends 0, 2 and 3 are unhappy. Friend 0 is counted once even though two different friends make it unhappy. Friend 1 is paired with their first choice and is happy. ### Constraints - `2 <= n <= 500` - `n` is even. - `preferences.length == n` - `preferences[i].length == n - 1` - `0 <= preferences[i][j] <= n - 1` - `preferences[i]` does not contain `i`. - All values in `preferences[i]` are unique. - `pairs.length == n / 2` - `pairs[i].length == 2` - `0 <= x_i, y_i <= n - 1` - Each person is contained in exactly one pair.

Constraints

  • 2 <= n <= 500
  • n is even.
  • preferences.length == n
  • preferences[i].length == n - 1
  • 0 <= preferences[i][j] <= n - 1
  • preferences[i] does not contain i.
  • All values in preferences[i] are unique.
  • pairs.length == n / 2
  • pairs[i].length == 2
  • 0 <= x_i, y_i <= n - 1
  • Each person is contained in exactly one pair.

Examples

Input: (4, [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]])

Expected Output: 2

Explanation: Source example 1: friends 1 and 3 prefer each other over partners 0 and 2, while 0 and 2 have their first choice.

Input: (2, [[1], [0]], [[1, 0]])

Expected Output: 0

Explanation: Minimum valid size from the source: n = 2, the single pair lists the larger member first, and each friend's only choice is the partner.

Hints

  1. Record each friend's partner first. A pair may list either member first, so store the pairing in both directions.
  2. For a friend x paired with y, only the friends that x places ahead of y can satisfy the first condition.
  3. Once any single u makes x unhappy, x is counted, and the remaining choices for x cannot change the answer.

Count Unhappy Friends After Pairing

mediumGeneralSoftware Engineer

There are n friends, labeled 0 to n - 1, where n is always even.

For each friend i, preferences[i] lists every friend other than i, sorted from most preferred to least preferred. A friend who appears earlier in the list is preferred over a friend who appears later.

All the friends are divided into pairs, given in the list pairs. Each pairs[i] = [x_i, y_i] means that x_i is paired with y_i and y_i is paired with x_i. Either member of a pair may be listed first.

A friend x who is paired with y is unhappy if there exists a friend u, paired with v, such that both of the following hold:

  • x prefers u over y, and
  • u prefers x over v.

Implement count_unhappy_friends(n, preferences, pairs) and return the number of unhappy friends. Each unhappy friend adds exactly 1 to the answer, even when several different choices of u make that friend unhappy.

The answer is an integer from 0 to n, so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Example 1

Input: n = 4, preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]], pairs = [[0,1],[2,3]]
Output: 2

Friend 1 prefers friend 3 over partner 0, and friend 3 prefers friend 1 over partner 2, so friend 1 is unhappy. For the same reason friend 3 is unhappy. Friends 0 and 2 are paired with their first choice and are happy.

Example 2

Input: n = 4, preferences = [[2,3,1],[0,2,3],[0,1,3],[0,1,2]], pairs = [[0,1],[2,3]]
Output: 3

Friend 0 prefers both 2 and 3 over partner 1, and both 2 and 3 rank 0 first, so friends 0, 2 and 3 are unhappy. Friend 0 is counted once even though two different friends make it unhappy. Friend 1 is paired with their first choice and is happy.

Constraints

  • 2 <= n <= 500
  • n is even.
  • preferences.length == n
  • preferences[i].length == n - 1
  • 0 <= preferences[i][j] <= n - 1
  • preferences[i] does not contain i.
  • All values in preferences[i] are unique.
  • pairs.length == n / 2
  • pairs[i].length == 2
  • 0 <= x_i, y_i <= n - 1
  • Each person is contained in exactly one pair.

Examples

Example 1
Input
(4, [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]])
Output
2
Notes
Source example 1: friends 1 and 3 prefer each other over partners 0 and 2, while 0 and 2 have their first choice.
Example 2
Input
(2, [[1], [0]], [[1, 0]])
Output
0
Notes
Minimum valid size from the source: n = 2, the single pair lists the larger member first, and each friend's only choice is the partner.

Constraints

  • •2 <= n <= 500
  • •n is even.
  • •preferences.length == n
  • •preferences[i].length == n - 1
  • •0 <= preferences[i][j] <= n - 1
  • •preferences[i] does not contain i.
  • •All values in preferences[i] are unique.
  • •pairs.length == n / 2
  • •pairs[i].length == 2
  • •0 <= x_i, y_i <= n - 1
  • •Each person is contained in exactly one pair.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_unhappy_friends(n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int:
Sample call
count_unhappy_friends(4, [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]])
Language
Case 1
Input
(4, [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], [[0, 1], [2, 3]])
Expected
2
Case 2
Input
(2, [[1], [0]], [[1, 0]])
Expected
0

Browse More Questions

Show the approach

Approach

Build two lookup tables first. partner[p] stores the friend p is paired with; each pair writes both directions, so the order inside a pair does not matter. rank_of[a][b] stores the position of b in preferences[a], so a prefers b over c exactly when rank_of[a][b] < rank_of[a][c]. Then, for each friend x with partner y, walk preferences[x] from the front. Every friend u met before y is someone x prefers over y. For each such u, with partner v, x is unhappy when rank_of[u][x] < rank_of[u][v]. As soon as one such u is found, add 1 and move on to the next x, which applies the rule that a friend is counted once no matter how many u qualify. Reaching y without a match means x is happy, because every later friend is one x likes less than y. Correctness: the scan visits exactly the friends u that satisfy the first condition, and the table answers the second condition in constant time, so x is counted if and only if the definition holds for some u. Edge cases: with n = 2 each friend's only choice is the partner, so the scan stops at once and the answer is 0. The comparison is strict, and it never compares a friend with itself, because u differs from y and therefore v differs from x. Building the tables takes O(n^2) time and space, and the scans take O(n^2) time in total.

Time complexity:
O(n^2)
Space complexity:
O(n^2)