Count Unhappy Friends
Real-world version: Restless Deckhands on Paired ShiftsThe statement here is our own: the same inputs, outputs and technique.
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:
awould rather work withcthan withbcwould rather work withathan withd
Return how many deckhands are restless.
Examples
Example 1
- Input
- n = 6, preferences = [[2, 1, 3, 4, 5], [3, 0, 2, 4, 5], [0, 3, 1, 4, 5], [1, 2, 0, 4, 5], [5, 0, 1, 2, 3], [4, 0, 1, 2, 3]], pairs = [[0, 1], [2, 3], [4, 5]]
- Output
- 4
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
- Input
- n = 4, preferences = [[2, 1, 3], [0, 2, 3], [0, 3, 1], [2, 0, 1]], pairs = [[0, 1], [2, 3]]
- Output
- 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
- Input
- n = 6, preferences = [[1, 2, 3, 4, 5], [0, 2, 3, 4, 5], [3, 0, 1, 4, 5], [2, 0, 1, 4, 5], [5, 0, 1, 2, 3], [4, 0, 1, 2, 3]], pairs = [[0, 1], [2, 3], [4, 5]]
- Output
- 0
Every deckhand is paired with the one at the top of their own list, so no swap could suit both sides.
Constraints
- 2 <= n <= 500
- n is even
- preferences.length == n
- preferences[i].length == n - 1
- 0 <= preferences[i][j] <= n - 1
- preferences[i] never names i
- The entries of preferences[i] are all different
- pairs.length == n / 2
- pairs[i].length == 2
- The two deckhands in a duo are different
- 0 <= pairs[i][0] <= n - 1
- 0 <= pairs[i][1] <= n - 1
- Every deckhand appears in exactly one duo
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Concatenation of ArrayReal-world version: Two Cycle PlayoutEasy
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Separate the Digits in an ArrayReal-world version: Digit Cells for the Ticket CodesEasy
- Distribute Elements Into Two Arrays IReal-world version: Splitting Parcels Onto Two Intake TraysEasy