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이어야 한다.
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를 오름차순으로 정렬한다.
citations = 1, 2, 3, 4, 6 index = 5, 4, 3, 2, 1 index- = 1, 2, 3, 4, 5
이걸 여사건으로 대입하면