[Programmers] H-index - JAVA

ONE·2022년 3월 23일
0

Programmers

목록 보기
19/24

📚 Problem

H-index

  • 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h최댓값 구하기

📝 Solution

Key Idea

  • 배열을 내림차순으로 정렬한 후, 인덱스보다 해당 인덱스의 배열값크거나 같은 인덱스최댓값을 구하면 됩니다

💻 Code

Solution.java

import java.util.Arrays;
import java.util.Collections;

class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        Integer[] array = Arrays.stream(citations).boxed().toArray(Integer[]::new);

        Arrays.sort(array, Collections.reverseOrder());

        for (int i = 0; i < array.length; i++)
            if(array[i] >= i + 1)
                answer = Integer.max(answer, i + 1);

        return answer;
    }
}

SolutionTest.java

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolutionTest {
    Solution solution;

    @BeforeEach
    public void setSol(){
        solution = new Solution();
    }

    @Test
    public void solution_1(){
        int result = solution.solution(new int[]{3, 0, 6, 1, 5});
        assertEquals(3, result);
    }
}

profile
New, Strange, Want to try

0개의 댓글