Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 실패율 #23

Closed
hwangJi-dev opened this issue Oct 11, 2022 · 0 comments
Closed

[Algorithm] 실패율 #23

hwangJi-dev opened this issue Oct 11, 2022 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Oct 11, 2022

💬 문제

[코딩테스트 연습 - 실패율](https://school.programmers.co.kr/learn/courses/30/lessons/42889)


💬 Idea

  • 스테이지 개수만큼 stageDict를 초기화해준다.
  • stage에 머무른 사용자의 수를 stageDict에 기록한다.
  • stage의 개수보다 큰 stage에 머무른 사용자는 filter를 통해 제거하고, 실패율을 계산한다.
    • 이 때 총 사용자의 수에서 낮은 stage부터 실패한 사용자가 누적으로 제거되어 실패율이 계산된다.
    • 즉 stage3에서 계산되는 실패율의 기준 사용자는
      기준 사용자 = 총 사용자의 수 - stage1,2에서 머무른 사용자의 수 이다.
  • 이렇게 실패율을 구하고, 실패율이 높은 스테이지부터 내림차순으로 스테이지의 번호가 담겨있는 배열을 return한다.

💬 풀이

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    var stageDict: [Int: Int] = [:]

    for i in 1...N {
        stageDict[i] = 0
    }
    
    for i in stages {
        stageDict[i] = stageDict[i] != nil ? stageDict[i]! + 1 : 1
    }
    
    var stageCount = stages.count
    var failureDict: [Int: Double] = [:]
    
    for i in stageDict.keys.filter({ $0 <= N }).sorted() {
        failureDict[i] = Double(stageDict[i]!)/Double(stageCount)
        stageCount -= stageDict[i]!
    }
    
    var sortedDict = failureDict.sorted { $0.key < $1.key }
    sortedDict = sortedDict.sorted { $0.value > $1.value }
    
    return sortedDict.map { $0.key }
}

💬 알게된 문법

⭐️ 나누기 할 때 소수점 이하의 숫자가 필요한 경우

Double(num1)/Double(num2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant