Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created November 30, 2020 14:54
Show Gist options
  • Save luojiyin1987/2b2c27eac2afdfe1a441e9ce3c64701e to your computer and use it in GitHub Desktop.
Save luojiyin1987/2b2c27eac2afdfe1a441e9ce3c64701e to your computer and use it in GitHub Desktop.
CountUnhappyFriends set
/**
* @param {number} n
* @param {number[][]} preferences
* @param {number[][]} pairs
* @return {number}
*/
var unhappyFriends = function(n, preferences, pairs) {
let arr = Array.from(Array(n), item=> new Array(n).fill(0));
for(let i=0; i<n; i++) {
preferences[i].forEach( (item, index) => {
arr[i][item] = index;
})
}
let res = new Set()
for(let i=0; i<=(n/2)-2; i++) {
for(let j=i+1; j<=(n/2)-1;j++) {
const [x,y] = pairs[i];
const [u,v] = pairs[j];
if(arr[x][u] < arr[x][y] && arr[u][x] < arr[u][v]) res.add(x).add(u)
if(arr[x][v] < arr[x][y] && arr[v][x] < arr[v][u]) res.add(x).add(v)
if(arr[y][u] < arr[y][x] && arr[u][y] < arr[u][v]) res.add(y).add(u)
if(arr[y][v] < arr[y][x] && arr[v][y] < arr[v][u]) res.add(y).add(v)
}
}
return res.size
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment