[programmers] 가장 많이 받은 선물
in Algorithm on Algorithm
https://school.programmers.co.kr/learn/courses/30/lessons/258712
import java.util.*;
class Solution {
public int solution(String[] friends, String[] gifts) {
int answer = 0;
Map<String,Friend> friend = new HashMap<>();
for(String name : friends){
friend.put(name, new Friend());
}
for(String gift : gifts){
String[] temp = gift.split(" ");
friend.get(temp[0]).add(temp[1]);
friend.get(temp[1]).minus();
}
for(String give : friends){
int tempCnt = 0;
for(String get : friends){
if(!give.equals(get)){
Friend give_person = friend.get(give);
Friend get_person = friend.get(get);
int give_cnt = give_person.m.getOrDefault(get,0);
int get_cnt = get_person.m.getOrDefault(give,0);
if( give_cnt > get_cnt || (give_cnt == get_cnt && give_person.index > get_person.index) )
tempCnt++;
}
}
answer = Math.max(tempCnt,answer);
}
return answer;
}
}
class Friend{
int index;
Map<String,Integer> m;
Friend(){
this.m = new HashMap<>();
index = 0;
}
void add(String name){
m.put(name, m.getOrDefault(name,0) + 1 );
index++;
}
void minus(){
index--;
}
}