Leetcode 1583: Count Unhappy Friends

grid47
grid47
Exploring patterns and algorithms
Jun 1, 2024 8 min read

You are given a list of preferences for n friends, where n is always even. Each person has a list of friends they prefer, and these friends are represented by integers from 0 to n-1. The friends are divided into pairs, where each pair is denoted by a list [xi, yi], meaning xi is paired with yi and yi with xi. However, some of the pairings may cause unhappiness. A person is unhappy if they prefer someone who is paired with someone else, and that person also prefers them over their current partner. Your task is to return the number of unhappy friends.
Problem
Approach
Steps
Complexity
Input: You are given an integer n representing the number of friends, a list preferences where preferences[i] is a list of integers representing the preferred friends for person i, and a list pairs where pairs[i] contains two integers denoting the pairing between two friends.
Example: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
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
• xi != yi
• 0 <= xi, yi <= n - 1
• Each person is contained in exactly one pair.
Output: Return the number of unhappy friends.
Example: Output: 2
Constraints:
Goal: To find the number of unhappy friends, we need to check each pairing to see if there is a more preferred person for one of the friends in the pair who also prefers them over their current partner.
Steps:
• 1. Create a ranking matrix to represent the preference order of each friend.
• 2. Traverse the list of pairs and for each pair, check if any friend in the pair would be happier with someone else who prefers them back over their current partner.
• 3. Keep track of the unhappy friends and return the count.
Goal: The problem has constraints related to the number of friends, size of the preferences list, and the number of pairs.
Steps:
• Ensure the solution can handle up to 500 friends efficiently.
• The solution should handle the scenario where all preferences are unique and no one prefers themselves.
Assumptions:
• The number of friends is always even.
• Each person in the pairs is distinct and paired with exactly one other person.
Input: Example 1: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
Explanation: In this example, Friend 1 is unhappy because they prefer Friend 3 over Friend 0, and Friend 3 prefers Friend 1 over Friend 2. Similarly, Friend 3 is unhappy because they prefer Friend 1 over Friend 2, and Friend 1 prefers Friend 3 over Friend 0. Therefore, the output is 2.

Input: Example 2: n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
Explanation: In this case, both Friend 0 and Friend 1 are happy because they are paired with each other and have no better alternative. Therefore, the output is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus