새소식

Problem solving/문제 풀이 - 2022.10.02

[파이썬] 프로그래머스 H-Index

  • -
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


나의 풀이

  • 각 h마다 추가되는 인용문 개수를 이분 탐색으로 찾아서 더했다.
from bisect import bisect_left, bisect_right

def solution(citations):
    citations.sort()
    # h ~ 0까지 가능
    cum_sum = dict(zip(range(len(citations)), [0] * len(citations)))
    # h보다 더 많이 인용된 논문 개수
    temp_sum = sum([1 for i in citations if i > len(citations)])
    for h in range(len(citations), -1, -1):
        right_idx = bisect_right(citations, h)     
        left_idx = bisect_left(citations, h)
        
        temp_sum += (right_idx - left_idx)
        # 최대 인용 순간
        if h <= temp_sum:
            break
            
    return h

 

다른 사람 풀이

  • 인덱스 값과 같거나 큰 수의 개수를 [ic]l - i[/ic]설정하고 인용수와 비교했다.
def solution(citations):
    citations = sorted(citations)
    l = len(citations)
    for i in range(l):
        if citations[i] >= l-i:
            return l-i
    return 0
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.