[프로그래머스/Java] Lv.2 H-Index

이은정·2024년 12월 1일

프로그래머스/Java

목록 보기
61/74

문제

풀이

먼저 citations를 정렬한다. answer에는 citations의 길이를 저장한다.
그 다음에 순서대로 citations의 값을 조회한다.
citations의 값이 answer보다 작다면 answer에서 1을 뺀다.
만약 citations의 값이 answer와 동일하거나 크다면 answer를 반환한다.

코드

import java.util.*;

class Solution {
    public int solution(int[] citations) {
        int len = citations.length;
        int answer = len;
        Arrays.sort(citations);
        
        for(int i = 0; i < len; i ++) {
            if (citations[i] >= answer) {
                return answer;
            }
            
            answer --;
        }
        return answer;
    }
}

결과

profile
돈 많은 백수가 꿈인 백엔드 개발자 지망생

0개의 댓글