publicclassSolution { publicintUnhappyFriends(int n, int[][] preferences, int[][] pairs) { int[,] order = newint[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { order[i, preferences[i][j]] = j; } } int[] match = newint[n]; foreach (int[] pair in pairs) { int person0 = pair[0], person1 = pair[1]; match[person0] = person1; match[person1] = person0; } int unhappyCount = 0; for (int x = 0; x < n; x++) { int y = match[x]; int index = order[x, y]; for (int i = 0; i < index; i++) { int u = preferences[x][i]; int v = match[u]; if (order[u, x] < order[u, v]) { unhappyCount++; break; } } } return unhappyCount; } }
classSolution { public: intunhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs){ vector<vector<int>> order(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { order[i][preferences[i][j]] = j; } } vector<int> match(n); for (constauto& pr: pairs) { match[pr[0]] = pr[1]; match[pr[1]] = pr[0]; }
int unhappyCount = 0; for (int x = 0; x < n; ++x) { int y = match[x]; int index = order[x][y]; for (int i = 0; i < index; ++i) { int u = preferences[x][i]; int v = match[u]; if (order[u][x] < order[u][v]) { ++unhappyCount; break; } } } return unhappyCount; } };
classSolution: defunhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: order = [[0] * n for _ inrange(n)] for i inrange(n): for j inrange(n - 1): order[i][preferences[i][j]] = j match = [0] * n for x, y in pairs: match[x] = y match[y] = x
unhappyCount = 0 for x inrange(n): y = match[x] index = order[x][y] for i inrange(index): u = preferences[x][i] v = match[u] if order[u][x] < order[u][v]: unhappyCount += 1 break return unhappyCount
intunhappyFriends(int n, int** preferences, int preferencesSize, int* preferencesColSize, int** pairs, int pairsSize, int* pairsColSize) { int order[n][n]; memset(order, 0, sizeof(order)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { order[i][preferences[i][j]] = j; } } int match[n]; memset(match, 0, sizeof(match)); for (int i = 0; i < pairsSize; ++i) { int* pr = pairs[i]; match[pr[0]] = pr[1]; match[pr[1]] = pr[0]; }
int unhappyCount = 0; for (int x = 0; x < n; ++x) { int y = match[x]; int index = order[x][y]; for (int i = 0; i < index; ++i) { int u = preferences[x][i]; int v = match[u]; if (order[u][x] < order[u][v]) { ++unhappyCount; break; } } } return unhappyCount; }
funcunhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) { order := make([][]int, n) for i, preference := range preferences { order[i] = make([]int, n) for j, p := range preference { order[i][p] = j } } match := make([]int, n) for _, p := range pairs { match[p[0]] = p[1] match[p[1]] = p[0] }
for x, y := range match { index := order[x][y] for _, u := range preferences[x][:index] { v := match[u] if order[u][x] < order[u][v] { ans++ break } } } return }
复杂度分析
时间复杂度:O(n^2)。 预处理需要填入二维数组 order 和数组 match 中的值,时间复杂度分别是 O(n^2) 和 O(n)。 统计不开心的朋友的数目时,需要遍历每个 x,找到满足要求的四元组 (x,y,u,v),其中遍历 u 的时间复杂度是 O(n),在已知 x 和 u 的情况下,可以在 O(1) 时间内得到 y 和 v,因此时间复杂度是 O(n^2)。 故总时间复杂度是 O(n^2)。
空间复杂度:O(n^2)。空间复杂度取决于预处理时创建的二维数组 order 和数组 match,其大小分别为 n \times n 和 n,因此空间复杂度是 O(n^2)。