Coding Test/Python

[Programmers][코딩테스트/정렬] H-Index

수수킴 2023. 7. 22.

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

[다른 풀이]

 

 

def solution(citations):
    citations.sort(reverse=True)
    answer = max(map(min, enumerate(citations, start=1)))
    return answer

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

citations.sort(reverse=True) [6, 5, 3, 1, 0]

sort로 정렬해서 가장 큰값부터 작은값으로 정렬한후,

enumerate(citations, start=1) [(1, 6), (2, 5), (3, 3), (4, 1), (5, 0)]

enumerate로 (index, value)형태로 묶는다.

min(1, 6), min(2, 5), min(3, 4), min(4, 1), min(5, 0)

해당 인용수 이상의 논문개수와 해당 논문의 인용수 중 더 작은 숫자를 고르는 작업을 하고(h-index로 가능한 숫자 추출)

max(1, 2, 3, 1, 0)

앞에서 골라진 (1, 2, 3, 1, 0) 중 가장 큰 숫자를 뽑아 실제 h-index를 구함.

 

프로그래머스

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

programmers.co.kr

 

댓글