FastPrepCount Unhappy Friends

Count Unhappy Friends

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

There are n people. preferences[x] lists every other person from most to least preferred, and pairs assigns everyone one partner.

Person x is unhappy if there exists u whom x prefers over x's partner y, and u prefers x over u's partner v. Return the number of unhappy people.

Function

unhappyFriends(n: int, preferences: int[][], pairs: int[][]) → int

Examples

Example 1

n = 4preferences = [[1,2,3],[3,2,0],[3,1,0],[1,2,0]]pairs = [[0,1],[2,3]]return = 2

People 1 and 3 satisfy the reciprocal preference condition.

Constraints

  • 2 <= n <= 500 and n is even.
  • Each preference row is a permutation of all other people.
  • Pairs form a perfect matching.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
  // Write your code here.
}
n4
preferences[[1,2,3],[3,2,0],[3,1,0],[1,2,0]]
pairs[[0,1],[2,3]]
expected2
Checking account…