[프로그래머스] 정렬 - H-Index(Python3)

kimgaeul02·2023년 1월 19일

프로그래머스

목록 보기
6/11

📄 H-Index

🔗 정렬 : H-Index



📌 해석

문제의 입력은 다음과 같다.

citations = [3, 0, 6, 1, 5]

이때 H-Index는 논문 n편 중 h편 이상 인용된 논문이 h편 이상임을 의미한다. 해당 citations를 살펴보면 n=5이다. H-index를 찾는 과정은 다음과 같다.

H_index = n-i

#1 i=0, n=5
if h_index = 5
citations[0] = 0
h편 이상: 2
h편 이하: 3

#2
if h_index = 4
citations[1] = 1
h편 이상: 2
h편 이하: 4

#3
if h_index = 3
citations[2] = 3
h편 이상: 3
h편 이하: 3

#4
if h_index = 2
citations[3] = 5
h편 이상: 3
h편 이하: 2

즉, H_index=3이다. citations를 오름차순 정렬한 뒤, 0~n까지 순회하며 H_index 값을 n-i로 가정한다. citations[i]>=H_index가 되면 반복문을 멈추고 answer에 H_index를 저장한다.


💻 코드

def solution(citations):
    answer = 0
    
    citations.sort()
    n=len(citations)
    
    for i in range(n):
        h_index=n-i
        if citations[i]>=h_index:
            answer=h_index
            break
    
    return answer

👨🏻‍💻 마무리

가장 큰 수... 못 풀겠다.

0개의 댓글