오늘의 문제 >> https://www.acmicpc.net/problem/25325
[ 생각 흐름 ]
이름을 먼저 HashMap에 담아놓고
for문을 돌면서 이름별로 카운트를 하면 되지 않을까?
[ JAVA ]
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String students = sc.nextLine();
String[] studentsArr = students.split(" ");
Map<String, Integer> countMap = new HashMap<>();
for(String name: studentsArr){
countMap.put(name, 0);
}
for(int i=0; i<n; i++){
String likeNames = sc.nextLine();
String[] likeNameArr = likeNames.split(" ");
for(String name:likeNameArr){
countMap.put(name, countMap.getOrDefault(name,0)+1);
}
}
Arrays.sort(studentsArr, (a,b) -> {
int countA = countMap.get(a);
int countB = countMap.get(b);
if (countA != countB){
return countB-countA;
}else{
return a.compareTo(b);
}
});
for(String name: studentsArr){
System.out.println(name+" "+countMap.get(name));
}
}
}
'TIL🔥 > 코딩테스트' 카테고리의 다른 글
[항해99클럽] Java 비기너_Day 17 Find the Distance Value Between Two Arrays (0) | 2025.04.22 |
---|---|
[항해99클럽] Java 비기너_Day 16 Intersection of Two Arrays (0) | 2025.04.21 |
[항해99클럽] Java 비기너_Day 14 브실이의 입시전략 (1) | 2025.04.17 |
[항해99클럽] Java 비기너_Day 13 단어 정렬 (0) | 2025.04.16 |
[항해99클럽] Java 비기너_Day 12 임스와 함께하는 미니게임 (0) | 2025.04.15 |
댓글