Giter Club home page Giter Club logo

algorithm's People

Contributors

yeahg-dev avatar

Watchers

 avatar

algorithm's Issues

[카카오]실패율 - 시간초과

🍉 문제

2019 카카오 블라인드 채용 - 실패율

🤨 트러블 슈팅

실패율을 구하기 위해 아래와 같이 stage를 순회하면서 각 stage에 대한 실패율을 계산한 배열을 리턴했다.

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    // 각 스테이지에 대한 실패율 계산
    // 스테이지 == 유저/유저 <= 스테이지
    let stageContainer: [Int] = Array(1...N)
    let result: [(Int, Double)] = stageContainer.map{(stage: Int) -> (Int, Double) in
        let plyersYetClear: Int = stages.filter{$0 == stage}.count
        let playersOnStage: Int = stages.filter{$0 >= stage}.count
        if playersOnStage == 0 {
            return (stage, 0)
        }
        let failureRate = Double(plyersYetClear) / Double(playersOnStage)
        return (stage, failureRate)
    }
    // 실패율 높은 스테이지부터 sort
    let stageResult = result.sorted{
                                    if $0.1 != $1.1 {
                                    return $0.1 > $1.1
                                    }
                                    return $0.0 < $1.0}.map{$0.0}
    return stageResult
}

실행 결과, 몇 개의 테스트에서 시간초과가 발생했다.

고민끝에 구글링을 하다가 https://keeplo.tistory.com/160 을 발견하게되었고,
stageContainer를 map으로 순회하면서 내부에서 또 stages를 순회해서 발생하는 문제임을 파악했다.

스테이지의 개수 N은 1 이상 500 이하의 자연수이다.
stages의 길이는 1 이상 200,000 이하이다.
위와 같은 조건에서 최악의 경우 스테이지개수 * stages의 길이는 500 * 200,000 로 매우 크다.

따라서 stages를 단 한번 순회하면서 실패율을 계산하는 로직으로 수정했다.

 func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    var stageRate: [Int] = [Int](repeating: 0, count: N + 1)
    for stage in stages {
        for i in 0..<stage {
            stageRate[i] += 1
        }
    }
    
    var result = [Int: Double]()
    for i in 0..<N {
        if Double(stageRate[i]) == 0 {
            result.updateValue(0, forKey:i+1)
            continue
        }
        let rate = Double(stageRate[i] - stageRate[i+1]) / Double(stageRate[i])
        result.updateValue(rate, forKey:i+1)
    }
    
    // 실패율 높은 스테이지부터 sort
    let stageResult = result.sorted{
                                    if $0.1 != $1.1 {
                                    return $0.1 > $1.1
                                    }
                                    return $0.0 < $1.0}.map{$0.0}
    return stageResult
}

🙌 오답 노트

  1. 중첩 반복문 또는 중첩 고차함수를 사용할 땐 시 간복잡도에 주의하자!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.