1583. Count Unhappy Friends

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1583

class Solution:
    def unhappyFriends(
        self, n: int, preferences: List[List[int]], pairs: List[List[int]]
    ) -> int:
        d = [{p: i for i, p in enumerate(v)} for v in preferences]
        p = {}
        for x, y in pairs:
            p[x] = y
            p[y] = x
        res = 0
        for x in range(n):
            y = p[x]
            res += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]])
        return res