Algorithm/프로그래머스
[Java] H-index
내영잉
2023. 4. 11. 11:38
1. 문제 📚
https://school.programmers.co.kr/learn/courses/30/lessons/42747
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
2. 입출력 예 📋
3. 알고리즘 ✅
1. 정렬해서 가장 큰 숫자 구하기
2. 인용된 논문의 개수와 인용되지 않은 논문 개수 세기
3. h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값을 구하기
4. 소스코드 💻
import java.util.*;
class Solution {
public int solution(int[] citations) {
Arrays.sort(citations);
int maxCit = citations[citations.length - 1];
int answer = 0;
for (int i = 0; i <= maxCit; i++) {
int h = i;
int cnt = 0; // h번 이상 인용된 논문 개수
int restCnt = 0; // h번 이하 인용된 논문 개수
for (int j = 0; j < citations.length; j++) {
if (citations[j] >= h) {
cnt++;
} else {
restCnt++;
}
}
// h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값
if (cnt >= h && restCnt <= h) {
answer = Math.max(h, answer);
}
}
return answer;
}
}