[프로그래머스] H-Index

O2o2✨·2020년 12월 18일
0

알고리즘

목록 보기
26/43

문제 링크 : 프로그래머스 - 정렬 > H-Index


풀이1(py)

def solution(citations):
    answer = 0
    citations.sort() 
    h = citations[-1]
    idx = 0
    
    while h >= 0:
        for i in range(len(citations)):
            if citations[i] >= h:
                idx = i
                break
        if len(citations) - idx >= h and idx <= h:
            return h
        
        h -= 1

    return answer

풀이2(js)

function solution(citations) {
    var n = Math.max(...citations);
    const LENGTH = citations.length;
    
    for (let h = n; h >= 0; h--){
        let cnt = citations.filter((citation) => citation >= h).length;
        if (cnt >= h && LENGTH - cnt <= h){
            return h;
        }        
    }
}
profile
프론트엔드 & 퍼블리셔

0개의 댓글