백준 2910 - 빈도 정렬

백준 2910 - 빈도 정렬

https://www.acmicpc.net/problem/2910

★ 풀이

hashMap을 활용해서 푸는 문제.

빈도수 대로 정렬하고 빈도수가 같을 경우 먼저 등장한 순서대로 정렬한다.

나중에 한번 더 볼만한 문제

★ 소스 코드

import java.io.*; import java.util.*; // 좋은 스킬 흡수하기 public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int n,m; public static void main(String[] args) throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); HashMap hm = new HashMap<>(); st = new StringTokenizer(br.readLine()); int idx = 0; for(int i = 0; i new Info(v.idx,++v.cnt)); } } ArrayList list = new ArrayList<>(hm.keySet()); Collections.sort(list, new Comparator() { public int compare(Integer a, Integer b) { if(hm.get(a).cnt == hm.get(b).cnt) { return hm.get(a).idx - hm.get(b).idx; }else return hm.get(b).cnt - hm.get(a).cnt;// value 기준 내림차순 정렬 } }); StringBuilder sb = new StringBuilder(); for(int x : list) { for(int i = 0; i

from http://sweet-smell.tistory.com/167 by ccl(A) rewrite - 2021-12-17 22:27:59