[프로그래머스 level2] H-Index python

IT공부중·2020년 3월 20일
0

알고리즘

목록 보기
6/49

https://programmers.co.kr/learn/courses/30/lessons/42747

def solution(citations):
    answer = 0
    citations.sort()
    h = 1
    while(h < len(citations)):
        temp = len(citations) - h
        if(temp !=0 and citations[temp] >= h and citations[temp-1] <= h):
            answer = max(answer,h)
        h += 1
    if(answer == 0 and citations[0] >= h):
        answer = h
    return answer

논문 n편 중 h번 이상 인용된 논문이 h편 이상이고 나머지 논문이 h번 이하여야한다.

그래서 정렬을 한다음에, h를 1부터 시작해서 계산한다.
temp를 len(citations)-h를 했을 때 citations[temp] >= h 라면
citations[temp]가 h보다 크니까 h편 이상인 논문이 h편 이상 개 있다고 볼 수 있다.
그리고 나머지 논문이 h번 이하 여야 되기 때문에 기준이 되는 citations[temp] 보다 index가 한 단계 낮은 citations[temp-1]이 h보다 작거나 같아야. 나머지 논문이 h번 이하 인용 되었다고 할 수 있다.

그리고 [10,50,100]일 때는 h번 이상 인용 된 논문이 h편 이상이어야 돼서 답이 3이 나오는데 위 while문에서는 걸러내지 못 한다. 그래서 마지막에 아직 답이 0일 때 첫번째 원소가 h보다 크거가 같으면 답은 h로 해줬더니 정답이 되었다. (테스트케이스 9번)

profile
3년차 프론트엔드 개발자 문건우입니다.

0개의 댓글