Amazon is releasing a feature called "Stuff Your Friends Are Buying".
Implement a function that returns stuff that a person’s friends are buying that the person has not bought, in order of popularity.
Returns a list of purchases of the person. If purchased multiple times, it will be in the list multiple times.
List<Purchase> getPurchases(Person person);
Returns a list of friends of the person.
List<Person> getFriends(Person person);
这道题要求根据某个人的好友购买记录,找出“朋友在买、但这个人自己没买过”的商品,并按热度排序。核心做法通常是先用哈希集合记录该人的所有购买,再遍历好友列表,统计好友购买过的商品频次;由于同一人可能重复购买,同一商品也可能在列表中重复出现,因此要按出现次数累加。最后过滤掉本人已经买过的商品,再按购买次数从高到低输出即可。