프로그래머스 Lv. 2 귤 고르기JAVA

YB·2024년 12월 25일

링크텍스트

설명

카운팅정렬을 이용해 풀었다. [1, 3, 2, 5, 4, 5, 2, 3]일 경우
counting[1]=1;
counting[2]=2;
counting[3]=2;
counting[4]=1;
counting[5]=2;
정렬해서 큰 수부터 차례대로 빼고 count가 k보다 크면 종료한다.
그런데 카운팅정렬로 푼 사람은 나밖에 없는거같다.

코드

import java.util.*;

class Solution {
    public int solution(int k, int[] tangerine) {
        int answer = 0;
        int count = 0;
        
        int [] counting = new int [10000001];
        
        for(int i=0;i<tangerine.length;i++){
            counting[tangerine[i]]++;
        }
        
        Arrays.sort(counting);
        
        for(int i= 10000000;i>0;i--){
            if(count>=k) break;
            if(counting[i]==0) continue;
            
            count+=counting[i];
            answer++;
        }
        
        return answer;
    }
}

다른 사람의 풀이

링크텍스트

profile
안녕하세요

0개의 댓글