IT/알고리즘
프로그래머스 H-Index
어센트
2020. 5. 19. 00:08
https://programmers.co.kr/learn/courses/30/lessons/42747
코딩테스트 연습 - H-Index
H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과1에 따르면, H-Index는 다음과 같이 구합니다. 어떤 과학자가 발표
programmers.co.kr
H-Index
- 먼저 크기 순으로 정렬을 한다 i=0 to arr[length-1]
- 논문인용 횟수가 가장 큰 값까지 순회한다. j=0 to length-1
- 논문 배열을 순회한다.
- 만약 순회하다 i보다 인용 수가 크거나 같으면 cnt1++
- 만약 순회하다 i보다 인용 수가 작거나 같으면 cnt2++
- if(cnt1>=i && i>=cnt2) => h = Math.max(i,h);
- 논문 배열을 순회한다.
풀이 코드
package lv2;
import java.util.Arrays;
public class H_index {
public static void main(String[] args) {
H_index app = new H_index();
int [] arr = {3,0,6,1,5};
System.out.println(app.solution(arr));
}
public int solution(int[] citations) {
int h = 0 ;
Arrays.parallelSort(citations);
for (int i = 0; i <=citations[citations.length-1] ; i++) { //논문 인용최대값까지
int cnt1 = 0,cnt2 = 0;
for (int j = 0; j < citations.length; j++) { //순회
if(citations[j]>=i){
cnt1++; //인용이 많은 경우
}
else{
cnt2++; //인용이적은 경우
}
}
if(cnt1>=i && cnt2<=i ) h = Math.max(i,h);
}
return h;
}
}