프로그래머스/파이썬

💫H-Index_알고리즘(정렬)

싱싱한복초이 2025. 1. 31. 01:58

https://school.programmers.co.kr/learn/courses/30/lessons/42747?language=python3

 

프로그래머스

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

programmers.co.kr

[문제 설명]

H-Index는 과학자의 생산성과 영향력을 나타내는 지표입니다. 어느 과학자의 H-Index를 나타내는 값인 h를 구하려고 합니다. 위키백과에 따르면, H-Index는 다음과 같이 구합니다.

어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값이 이 과학자의 H-Index입니다.

어떤 과학자가 발표한 논문의 인용 횟수를 담은 배열 citations가 매개변수로 주어질 때, 이 과학자의 H-Index를 return 하도록 solution 함수를 작성해주세요.

 

제한사항

  • 과학자가 발표한 논문의 수는 1편 이상 1,000편 이하입니다.
  • 논문별 인용 횟수는 0회 이상 10,000회 이하입니다.

https://en.wikipedia.org/wiki/H-index

 

h-index - Wikipedia

From Wikipedia, the free encyclopedia Metric that attempts to measure the productivity and citation impact of a person's publications The h-index is an author-level metric that measures both the productivity and citation impact of the publications, initial

en.wikipedia.org

 

[나의 풀이]

def solution(citations):
    citations.sort(reverse = True)
    for i in range(len(citations)):
        if citations[i] < i + 1:
            return i # 조건이 깨지면 그 이전까지의 i가 h-index
    return len(citations) # 모든 논문이 조건을 만족하는 경우

 

 

논문 인용 횟수를 순회하면서 검사해야 할 조건은 바로 >>현재 논문이 순위 i (인덱스 숫자)+1 이상 인용되었는가?<< 이다.

수식으로 citations[i] >= i+1 조건을 만족하는지 확인하는 것이다.

따라서  citations[i] < i+1  일때를 구해서 i + 1의 직전 값인 i를 return하면 된다.

 

[다른 풀이]

def solution(citations):
    citations.sort(reverse=True)
    answer = max(map(min, enumerate(citations, start=1)))
    return answer