1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/138476

 

2. ํ’€์ด

ํ•ด์‰ฌ๋งต์— ๋„ฃ์–ด์„œ ์ •๋ ฌ ํ›„ ์ข…๋ฅ˜๋ณ„ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์€ ๊ฒƒ ๋ถ€ํ„ฐ ์ƒ์ž์— ๋‹ด์•„์„œ ์นด์šดํŠธ ํ•ด์ค€๋‹ค.

 

3. ์ฝ”๋“œ

import java.util.*;

class Solution {
    public int solution(int k, int[] tangerine) {
        HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
        
        for(int i=0; i<tangerine.length; i++){
            hm.put(tangerine[i], hm.getOrDefault(tangerine[i],0)+1);
        }
        
        List<Map.Entry<Integer, Integer>> entList = new ArrayList<Map.Entry<Integer, Integer>>(hm.entrySet());

		// ๋น„๊ตํ•จ์ˆ˜ Comparator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด๋ฆผ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
		Collections.sort(entList, new Comparator<Map.Entry<Integer, Integer>>() {
			// compare๋กœ ๊ฐ’์„ ๋น„๊ต
			public int compare(Map.Entry<Integer, Integer> obj1, Map.Entry<Integer, Integer> obj2)
			{
				// ๋‚ด๋ฆผ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
				return obj2.getValue().compareTo(obj1.getValue());
			}
		});
        
        int[] answer = new int[entList.size()];
        int cnt = 0;
        
		// ๊ฒฐ๊ณผ 
		for(Map.Entry<Integer, Integer> entry : entList) {
			k = k-entry.getValue();
            cnt++;
            if(k<=0) break;
            
		}
        return cnt;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ