H-Index

이민호·2021년 3월 24일

나의 문제 풀이

원리

h-index는 citations의 요소가 index번호보다 작거나 크면 조건에 만족된다.
이중 index의 최대값이 h-index이다.

예 citations = [5, 6, 8, 3, 9]

citations        index
   9               1
   8               2
   6               3
   5               4
   3               5

여기서 index보다 작은 citations의 숫자는 3이다.
그러므로 citations >= index를 만족하는 citations의 가장 작은 수는 5이므로 5의 index번호인 4가 h-index가 된다.

def solution(citations):
    answer = 0
    index =1
    citations.sort(reverse=True)
    if citations[0] > 0:
        for cit in citations:
            if cit < index:
                answer = index
                break
            else:
                index += 1
        answer = index -1
    return answer

주의할점
1. 모든 citations의 요소가 0일때
-> 모든요소가 0이면 h-index도 존재하지 않으므로 0이어야 한다.

  1. 모든 citations의 요소가 citations >= index를 만족할 때
    -> for문이 모두 완료하면 index값은 실재보다 1이 더 크기때문에 -1을 해준다.

다른사람의 풀이

def solution(citations):
    citations = sorted(citations)
    l = len(citations)
    for i in range(l):
        if citations[i] >= l-i:
            return l-i
    return 0

이 문제 풀이 방식은 여사건을 이용하여 풀이하였다.

예) citations = [6, 4, 3, 2, 1]
1. citations를 오름차순으로 정렬한다.

  1. index- = index의 여사건(l - i)
citations = 1, 2, 3, 4, 6
    index = 5, 4, 3, 2, 1
   index- = 1, 2, 3, 4, 5
  • H-Index = citations[i] >= index를 만족하는 index의 최대값이다.

이걸 여사건으로 대입하면

  • H-Index = citations[i] >= index-를 만족하는 index의 최소값이다.
profile
life is fun

0개의 댓글