본문 바로가기
#알고리즘 [Algorithm]/Problem

[프로그래머스 - 정렬] H-Index

by cy_mos 2019. 4. 5.
반응형

[프로그래머스 - 정렬] H-Index


📄 [정렬] H-Index Swift Source Code

import Foundation

// MARK: - https://en.wikipedia.org/wiki/H-index
func solution(_ citations:[Int]) -> Int {
    
    let length = citations.count
    
    // MARK: - First we order the values of f from the largest to the lowest value. (DESC - 오름차순)
    let paper = citations.sorted { $0 > $1 }
        
    // MARK: - we look for the last position in which f is greater than or equal to the position (we call h this position).
    var hIndex: Int = 0
    for index in 0..<length where paper[index] >= index + 1 { hIndex = hIndex + 1 }
    
    return hIndex
}

🚀 REFERENCE

반응형

댓글