Idealim

문제

  • https://school.programmers.co.kr/learn/courses/30/lessons/42747
  • H-Index( 과학자의 생산성과 영향력을 나타내는 지표)를 구하는 문제
    • H-Index : 어떤 과학자가 발표한 논문 n편 중, h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하 인용되었다면 h의 최댓값

Solution

중요 Idea

  • 아무리 인용수(citations) 값이 크더라도 논문 편수가 작으면 H-index 는 작을 수 밖에 없다.
    • ex. [312,521,1021] ⇒ H-index = 3

코드 1

def solution(citations):
    citations.sort(reverse=True)
    for h, citation in enumerate(citations, start=1):
        # 현재 h값과 인용 횟수 비교
        if h > citation:
            break

    return h - 1
  • 정렬을 하는 이유 : 인용수보다 큰 개수를 따로 구하지 않아도 되기 때문이다.
  • 테스트 9에서 실패
    ⇒ 왜 실패?
  • 참고자료 : https://liveloper-jay.tistory.com/140?category=93899
  • 모든 인용횟수가 같을 경우에는 citations 길이를 반환해야 됨.
    • ex. [6, 6, 6, 6] ⇒ 4 반환해야지만 위 코드 기준 3을 반환한다.

코드 2

def solution(citations):
    citations.sort(reverse=True)
    for h, citation in enumerate(citations, start=1):
        # 현재 h값과 인용 횟수 비교
        if h > citation:
            break
        # citations 다 같은 경우 ex. [6,6,6,6]
        if h == len(citations): 
            return len(citations)

    return h - 1
  • citations 의 element 다 같은 경우 따로 처리
반응형
profile

Idealim

@Idealim

읽어주셔서 감사합니다. 잘못된 내용이 있으면 언제든 댓글로 피드백 부탁드립니다.