NOTE/SWEA
7102. 준홍이의 카드놀이 - d3
m-inz
2022. 6. 10. 12:58
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int T;
T=Integer.parseInt(sc.nextLine());
StringBuilder sb = new StringBuilder();
StringTokenizer st;
for(int test_case = 1; test_case <= T; test_case++)
{
st=new StringTokenizer(sc.nextLine()," ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=1; i<=N; i++) {
for(int j=1; j<=M; j++) {
if(hm.containsKey(i+j)) {
hm.put(i+j,hm.get(i+j)+1);
}else
hm.put(i+j,1);
}
}
ArrayList<Integer> al = new ArrayList<>(hm.keySet());
Collections.sort(al,(o1,o2)->hm.get(o2).compareTo(hm.get(o1)));
int max = hm.get(al.get(0));
sb.append("#"+test_case+" ");
for(int i: al) {
if(max==hm.get(i))
sb.append(i+" ");
else break;
}
sb.append('\n');
}
System.out.println(sb);
}
}
*HashMap -> value 기준으로 정렬하기
ArrayList<Integer> al = new ArrayList<>(hm.keySet());
keySet을 al에 담고
Collections.sort(al,(o1,o2)->hm.get(o2).compareTo(hm.get(o1)));
정렬!
+ 내림차순으로 하고싶으면
Collections.sort(al,(o1,o2)->hm.get(o1).compareTo(hm.get(o2)));
이렇게 되면 al에 value 기준으로 정렬한대로 key들이 존재하게 된다 ~_~
따라서 al.get(0) 이게 value 젤 높은 key 값!
-> 를 이용해서 쓰는게 직관적이고 깔끔해보여서 앞으로 이렇게 쓰려고 한다 😀