leetcode1583. 统计不开心的朋友

时间:2021-08-14
本文章向大家介绍leetcode1583. 统计不开心的朋友,主要包括leetcode1583. 统计不开心的朋友使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目链接:https://leetcode-cn.com/problems/count-unhappy-friends/
这题其实做起来比较顺,按照正常思路就可以AC。
讲一下我一开始的思路:
题目不难理解,结合示例看一下很好懂,求解的是不开心的朋友的数目,那么肯定要遍历每一位朋友,如果符合条件就++,不符合就跳过,那么再看具体的判断过程。
大概步骤为:获取当前朋友的配对队友->判断该配对队友是否是当前朋友亲近程度最高的->如果是证明该朋友是开心的,跳过;如果不是,则判断亲近程度比配对队友高的朋友中是否有符合条件的。
关键的两步是获取配对的队友以及比较亲近程度,配对队友可以通过直接遍历pairs来获得,也可以提前预处理把配对关系放进数组里。

这里给出两段代码:

遍历pairs:

class Solution {
public:
    int findPair(vector<vector<int>>&pairs,int k){//寻找u的配对
        int i=0;
        for(;i<pairs.size();i++){
            if(pairs[i][0]!=k&&pairs[i][1]!=k) continue;
            else break;
        }
        return pairs[i][0]==k?pairs[i][1]:pairs[i][0];
    }
	//判断是否开心
    bool judge(vector<vector<int>>& preferences,vector<vector<int>>& pairs,int x,int y){
        int i=0;
        while(preferences[x][i]!=y){
            int u=preferences[x][i];//依次遍历x的好友中比x与y亲近程度更高的好友u
            vector<int>it=preferences[u];
            int v=findPair(pairs,u);//寻找u的配对好友
            auto pos1=find(it.begin(),it.end(),x);//在u中找到x的位置
            auto pos2=find(it.begin(),it.end(),v);//在u中找到v的位置
            if(pos1-pos2<0) return false;//判断u中x、v的亲密程度,通过比较距离判断
            i++;
        }
        return true;
    }
    int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
        int ans=0;
        for(auto&pair:pairs){//遍历pairs
            int x=pair[0],y=pair[1];
            if(!judge(preferences, pairs, x, y)) ans++;//判断x是否开心
            if(!judge(preferences, pairs, y, x)) ans++;//判断y是否开心
        }
        return ans;
    }
};

预处理:

class Solution {
public:
    int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
        //只有两个人必然都开心
        if(n == 2)  return 0;

        //用一个二维数组存放亲近程度,order[i][j]表示第i位朋友
        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(int i = 0; i < n/2; i++){
            auto 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];
            //index表示y在x亲近程度表的下标
            int index = order[x][y];
            //小于index的朋友都有可能是目标对象,逐个排查
            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;
    }
};

原文地址:https://www.cnblogs.com/LingFengJ/p/15140622.html