Trains the technique from
LeetCode 1583Count Unhappy FriendsThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A crew of n deckhands is split into shift duos. preferences[i] lists every other deckhand in the order deckhand i would rather work with them, best first, so it holds n - 1 names and never names i. pairs gives the duos, and every deckhand appears in exactly one duo.
Deckhand a, paired with b, is restless when some deckhand c, paired with d, satisfies both of these at once:
a would rather work with c than with bc would rather work with a than with dReturn how many deckhands are restless.
Example 1
Deckhands 0 and 2 top each other's lists yet are paired elsewhere, and so do 1 and 3, which leaves all four of them restless. Deckhands 4 and 5 are each other's first choice and are already paired, so neither is.
Example 2
Deckhand 0 would rather work with 2 than with 1, and 2 would rather work with 0 than with 3, so both are restless. Deckhands 1 and 3 already have their first choice.
Example 3
Every deckhand is paired with the one at the top of their own list, so no swap could suit both sides.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def unhappy_friends(n: int, preferences: list[list[int]], pairs: list[list[int]]) -> int:public int unhappyFriends(int n, int[][] preferences, int[][] pairs)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.