电面面经:Square

问题输入:
List<List<String>> of
(“Apple, Orange, Pear, Flour, Rice, Corn, leftWin”);
(“Flour,Corn, Rice, Grape, Cherry, Orange, rightWin”);
(“Rice, Flour, Corn, Grape, Orange, Cherry, rightWin”);

每一份Recipe由三个成分组成,每个match有两个Recipe,厨师每次挑选一个Recipe,定出胜负。
经过n次比赛后,求前k个胜出最多的Recipe。

public void printTopKWin(List<List<String>> matches, int k){

}

打印结果即可。

2 Likes

Map<triple, times>,再排序输出前k个

感谢楼主分享

这个月的锦鲤非楼主莫属啦 :blush:

1 Like

看也看不懂这题什么意思

triple是啥语言?Python?

Tuple 的意思吧,see

我定义了一个Recipe class:

public class Recipe {
    private String firstIngredient;
    private String secondIngredient;
    private String thirdIngredient;

    public Recipe(String first, String second, String third){
    }
   
   public boolean equals(Object obj) {
        //Override
    }
    public int hashCode(){
    //Override
   }
}

然后生成 Map<Recipe, Integer> recipeWinCountMap

:sweat:

就是receipt就好了,三个string,自己定义一个class就行

你这输入是啥意思,leftWin rightWin又表示什么

随便写了一个

    public static void printTopKWin(List<List<String>> matches, int k){
        if (matches == null || matches.size() == 0) return;

        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < matches.size(); i++) {
            List<String> match = matches.get(i);
            String key = "";
            if ("leftWin".equals(match.get(6))) {
                key = match.get(0) + "/" + match.get(1) + "/" + match.get(2);
            } else {
                key = match.get(3) + "/" + match.get(4) + "/" + match.get(5);
            }
            map.put(key, map.getOrDefault(key, 0) + 1);
        }
        PriorityQueue<String> pq = new PriorityQueue<>((a, b) -> (map.get(a) - map.get(b)));
        for (String key : map.keySet()) {
            pq.offer(key);
            if (pq.size() > k) pq.poll();
        }
        while (!pq.isEmpty())
            System.out.println(pq.poll());
    }

“Apple, Pear, Orange”, “Apple, Orange, Pear”, “Pear, Orange, Apple” 同属一个Recipe

这个equals的写法可以先collections sort,然后挨个比较string

match.get(0).hashCode() + match.get(1).hashCode() + match.get(2).hashCode()

1 Like
Set<String> myValues = new HashSet<>();
myValues.add(firstIngredient);
myValues.add(secondIngredient);
myValues.add(thirdIngredient);

if(!myValues.contain(other.firstIngredient)){
    return false;
}
if(!myValues.contain(other.secondIngredient)){
    return false;
}
if(!myValues.contain(other.thirdIngredient)){
    return false;
}
return true;
1 Like

这样呀,你的方法当然可以,这个类比较简单,可以单独写,看面试官喜好了
我改了一下我刚才的code

    public static void printTopKWin(List<List<String>> matches, int k){
    if (matches == null || matches.size() == 0) return;

    Map<Set<String>, Integer> map = new HashMap<>();
    for (int i = 0; i < matches.size(); i++) {
        List<String> match = matches.get(i);
        Set<String> key = new HashSet<>();
        if ("leftWin".equals(match.get(6))) {
            key.add(match.get(0));
            key.add(match.get(1));
            key.add(match.get(2));
        } else {
            key.add(match.get(3));
            key.add(match.get(4));
            key.add(match.get(5));
        }
        map.put(key, map.getOrDefault(key, 0) + 1);
    }
    PriorityQueue<Set<String>> pq = new PriorityQueue<>((a, b) -> (map.get(a) - map.get(b)));
    for (Set<String> key : map.keySet()) {
        pq.offer(key);
        if (pq.size() > k) pq.poll();
    }
    while (!pq.isEmpty())
        System.out.println(pq.poll());
}