# 电面面经：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”);

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

}
``````

2 Likes

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

1 Like

triple是啥语言？Python?

Tuple 的意思吧，see

``````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
}
}
``````

``````    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

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

1 Like
``````Set<String> myValues = new HashSet<>();

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

``````    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))) {
} else {