상세 컨텐츠

본문 제목

[프로그래머스] H-Index

💯ProblemSolving/문제 풀이-Java

by :부셔져버린개발자 2024. 12. 12. 11:12

본문

       문제 요약        

- 문제

https://school.programmers.co.kr/learn/courses/30/lessons/42747

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

- 입출력

citations [3, 0, 6, 1, 5] return 3

 

         아이디어        

h 를 구하는 문제

정렬 후 , 이분 탐색으로 구하기

 left <= right : mid값이 정답이 될 수도 있으므로 

 

 

         소스코드        

import java.util.*;
class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        Arrays.sort(citations);
        int left = 0, right = 10000; // h 편 설정
        
        while(left <= right) {
            int h = (right - left) / 2 + left;
            if(isValid(citations, h)) {
                answer = Math.max(answer, h);
                left = h + 1;
            } else {
                right = h - 1;
            }
            
        }
        return answer;
    }
    
    public boolean isValid(int[] citations, int h) {
        int cnt = 0;
        for(int i = 0; i < citations.length; i++) {
            if(citations[i] >= h) {
                cnt += 1;
            }
        }
        return cnt >= h;
    }
}

 

 

728x90

관련글 더보기