其他分享
首页 > 其他分享> > 【leetcode】1583. Count Unhappy Friends

【leetcode】1583. Count Unhappy Friends

作者:互联网

题目如下:

You are given a list of preferences for n friends, where n is always even.

For each person ipreferences[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] = [xi, yi] denotes xi is paired with yi and yi is paired with xi.

However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but:

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

Return the number of unhappy friends.

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
Explanation:
Friend 1 is unhappy because:
- 1 is paired with 0 but prefers 3 over 0, and
- 3 prefers 1 over 2.
Friend 3 is unhappy because:
- 3 is paired with 2 but prefers 1 over 2, and
- 1 prefers 3 over 0.
Friends 0 and 2 are happy.

Example 2:

Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
Output: 0
Explanation: Both friends 0 and 1 are happy.

Example 3:

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

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.

解题思路:本题很简单,只要为每个朋友分配一个权值即可,grid_preferences[i][j] = v 表示对于i来说,朋友j的权值是v,v值越小越亲近。

代码如下:

class Solution(object):
    def unhappyFriends(self, n, preferences, pairs):
        """
        :type n: int
        :type preferences: List[List[int]]
        :type pairs: List[List[int]]
        :rtype: int
        """
        dic_unHappy = {}

        grid_preferences = [[0] * n for _ in range(n)]

        for i in range(len(preferences)):
            for j in range(len(preferences[i])):
                grid_preferences[i][preferences[i][j]] = j

        for i in range(len(pairs)):
            for j in range(len(pairs)):
                if i == j:continue
                x = pairs[i][0]
                y = pairs[i][1]
                u = pairs[j][0]
                v = pairs[j][1]

                '''
                (x,y), (u,v)
                x is unhappy if x is paired with y and there exists a friend u who is paired with v but:
                    x prefers u over y, and
                    u prefers x over v.
                
                '''
                if grid_preferences[x][u] < grid_preferences[x][y] and grid_preferences[u][x] < grid_preferences[u][v]:
                    dic_unHappy[x] = 1

                if grid_preferences[x][v] < grid_preferences[x][y] and grid_preferences[v][x] < grid_preferences[v][u]:
                    dic_unHappy[x] = 1

                if grid_preferences[y][u] < grid_preferences[y][x] and grid_preferences[u][y] < grid_preferences[u][v]:
                    dic_unHappy[y] = 1

                if grid_preferences[y][v] < grid_preferences[y][x] and grid_preferences[v][y] < grid_preferences[v][u]:
                    dic_unHappy[y] = 1

        return len(dic_unHappy)

 

标签:Count,pairs,preferences,1583,over,paired,grid,Friends,prefers
来源: https://www.cnblogs.com/seyjs/p/14793733.html