- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/258712
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/가장 많이 받은 선물
풀이 시간 : 68분
import java.util.*;
class Solution {
public int solution(String[] friends, String[] gifts) {
int n = friends.length;
Map<String, Integer> friendIndex = new HashMap<>();
for (int i = 0; i < n; i++) {
friendIndex.put(friends[i], i);
}
int[] giftDegree = new int[n];
int[][] giftGraph = new int[n][n];
for (String gift : gifts) {
String[] parts = gift.split(" ");
int giver = friendIndex.get(parts[0]);
int receiver = friendIndex.get(parts[1]);
giftDegree[giver]++;
giftDegree[receiver]--;
giftGraph[giver][receiver]++;
}
int maxCount = 0;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (i != j && (giftGraph[i][j] > giftGraph[j][i] ||
(giftGraph[i][j] == giftGraph[j][i] && giftDegree[i] > giftDegree[j]))) {
count++;
}
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
}