ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【leetcode】1583. Count Unhappy Friends

2021-05-21 15:02:27  阅读:273  来源: 互联网

标签:Count pairs preferences 1583 over paired grid Friends prefers


题目如下:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有