[프로그래머스/LV2] H-Index - 정렬 (Java)
프로그래머스 LV2 H-Index 문제를 Java로 해결한 풀이입니다. 정렬을 활용한 조건문 풀이로 설명합니다.
[프로그래머스/LV2] H-Index
🙋♂️ 들어가며
정렬을 먼저 해보자 그리고 어떻게 해야 H-Index를 구하는지도 보고
1
2
citations = {0, 1, 3, 5, 6};
idx = {0, 1, 2, 3, 4};
아 H-Index = citations길이 - i 겠구나.
✅ 정답 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
class Solution {
public int solution(int[] citations) {
int answer = 0;
int N = citations.length;
Arrays.sort(citations);
for (int i = 0; i < N; i++) {
int h = N - i;
if (citations[i] >= h) {
answer = h;
break;
}
}
return answer;
}
}
This post is licensed under CC BY 4.0 by the author.
