본문 바로가기
코테/프로그래머스

[프로그래머스] H-Index 스위프트

by 리드맥 2022. 3. 1.
func solution(_ citations:[Int]) -> Int {
    
    var arr:[Int] = []
    var result:[Int] = []
    result.append(0)
    arr.append(0)
    
    var citations = citations.sorted(by: <)
//    print(citations)
    
    
        for i in 0...citations.count - 1 {
            var hab = citations.count - i
//            print("hab\(hab)")
            
            
            for j in 0...citations[i]{
//                print(j)
                if j >= arr.last! && j <= hab {
                    result.append(j)
                }
            }
            arr.append(citations[i])

    }
    
//    print(result)
    
    return result.last!
}

[5,5,5,5] 배열의 답은 4입니다. max 함수를 사용하나 last 를 사용하나 답은 같은데 max 시간이 압도적으로 많이 걸려서 시간초과가 났습니다

댓글