friend recommend

function get_friends(A) 能得到A的所有朋友,求get_friends_of_friends(A)

口头跑程序,run了好多个case,一开始我把所有和A有关系的“朋友”都找出来了,后来面试官提示只要找A的朋友的朋友(2层)并且找到的不能含A的朋友。就是一个图的搜索,并记录图的深度degree......

//return friends of friends that are not this person's friends

//Friend Recommendation:bucket sort,O(m) time,O(n) space,m is num of person's friends' friends,n is num of valid friends
public class Solution {
    public class Person {
        int id;
        HashSet<Integer> friends = new HashSet<>();
    }

    private List<Integer> friendsRecommend(Person person, int k) {
        List<Integer> res = new ArrayList<>();
        if (person == null) {
            return res;
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int friend : person.friends) {
            for (int recommend : friend.friends) {
               int id = recommend.id;
               if (person.friends.contains(id) || id == person.id) {
                   continue;//if person already has this friend,or this is person himself/herself,continue
               }
               if (!map.containsKey(id)) {
                   map.put(id, 0);
               }
               map.put(id, map.get(id) + 1);
           }
        }
        List<List<Integer>> buckets = new ArrayList<>();
        for (int i = 0; i <= nums.length; i++) {
            buckets.add(new ArrayList<Integer>());//we use frequency as index, so <= length
        }
        for (int key : map.keySet()) {//unlike heap solution, we only need keySet() here
            buckets.get(map.get(key)).add(key);//get the id(key), add the id to its frequency bucket
        }
        for (int i = buckets.size() - 1; i >= 0; i--) {
            for (int j = 0; j < buckets.get(i).size(); j++) {//start adding the vals at the last bucket's last index
                res.add(buckets.get(i).get(j));
                if (res.size() == k) {
                    return res;//this two loops are O(k) time, when res has k nums, return it
                }
            }
        }
        return res;
    }
}

//Friend Recommendation: Quick Select, average O(m + n) time, O(1) space, worst case O(m + n^2) time
//m is num of person's friends' friends,n is num of valid friends
public class Solution {
    public class Person {
        int id;
        HashSet<Integer> friends = new HashSet<>();
    }

    private List<Integer> friendsRecommend(Person person, int k) {
        List<Integer> res = new ArrayList<>();
        if (person == null) {
            return res;
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int friend : person.friends) {
            for (int recommend : friend.friends) {
               int id = recommend.id;
               if (person.friends.contains(id) || id == person.id) {
                   continue;//if person already has this friend,or this is person himself/herself,continue
               }
               if (!map.containsKey(id)) {
                   map.put(id, 0);
               }
               map.put(id, map.get(id) + 1);
           }
        }
        
        ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        int left = 0;
        int right = list.size() - 1;
        int index = -1;
        while (true) {
            int pos = partition(list, left, right);
            if (pos + 1 == k) {
                index = pos;
                break;
            } else if (pos + 1 > k) {
                right = pos - 1;
            } else {
                left = pos + 1;
            }
        }
        if (index == -1) {
            return res;
        }
        for (int i = 0; i <= index; i++) {
            int id = list.get(i).getKey();
            res.add(id);
        }
        return res;
    }
    
    private int partition(ArrayList<Map.Entry<Integer, Integer>> list, int left, int right) {
        Random rand = new Random();
        int index = rand.nextInt(right - left + 1) + left;//remember to add + left !!!
        Map.Entry<Integer, Integer> pivot = list.get(index);
        int pVal = pivot.getValue();
        swap(list, left, index);//index, not pivot !!
        int l = left + 1;
        int r = right;
        while (l <= r) {
            int lVal = list.get(l).getValue();
            int rVal = list.get(r).getValue();
            if (lVal < pVal && rVal > pVal) {
                swap(list, l, r);
            }
            if (lVal >= pVal) {
                l++;
            }
            if (rVal <= pVal) {
                r--;
            }
        }
        swap(list, left, r);
        return r;
    }
    
    private void swap(ArrayList<Map.Entry<Integer, Integer>> list, int left, int right) {
        Map.Entry<Integer, Integer> temp = list.get(left);
        list.set(left, list.get(right));
        list.set(right, temp);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,281评论 0 23
  • 昨晚做了个梦,梦到你们了...很开心,醒来之后却很心酸,终究只是个梦,我也希望自己能被你说服...
    尘言呓语阅读 52评论 0 0
  • 我一直等到那些狗再也看不见了,才离开了秋芽子的坟,回到家中,胡乱吃了点东西,倒头就睡,这一天对我来说,不论是从身体...
    喵丿大王阅读 344评论 0 0
  • 突然想起来昨晚躺在床上脑海里一闪而过的问题。林徽因一生受尽三个男人专宠。胡兰成一生狐妖般爱尽各种女人。那这两个“祸...
    红豆红豆锁阅读 377评论 0 0