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] 모의고사 #57

Closed
hwangJi-dev opened this issue Jan 5, 2023 · 0 comments
Closed

[Algorithm] 모의고사 #57

hwangJi-dev opened this issue Jan 5, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 5, 2023

💬 문제

[코딩테스트 연습 - 모의고사](https://school.programmers.co.kr/learn/courses/30/lessons/42840)


💬 Idea

  • 수포자 1,2,3의 정답 배열을 만든다. (answers보다 길도록)
  • answer 배열을 돌면서 supoza1,2,3과 정답이 같은 경우에 dict에 해당 점수를 더한다.
  • dict의 max value값과 같은 value를 가진 key만을 filter링하여 정답으로 도출한다.

💬 풀이

import Foundation

func solution(answers:[Int]) -> [Int] {
    let supoza1 = Array(String(repeating: "12345", count: (answers.count / 5) + 1)).map({ String($0) })
    let supoza2 = Array(String(repeating: "21232425", count: (answers.count / 8) + 1)).map({ String($0) })
    let supoza3 = Array(String(repeating: "3311224455", count: (answers.count / 10) + 1)).map({ String($0) })
    var dict: [Int: Int] = [1: 0, 2: 0, 3: 0]
    
    for (index, answer) in answers.enumerated() {
        dict[1] = supoza1[index] == String(answer) ? dict[1]! + 1 : dict[1]
        dict[2] = supoza2[index] == String(answer) ? dict[2]! + 1 : dict[2]
        dict[3] = supoza3[index] == String(answer) ? dict[3]! + 1 : dict[3]
    }
    
    return dict.filter({ $0.value == dict.values.max() }).keys.sorted()
}

💬 더 나은 풀이

import Foundation

func solution(answers:[Int]) -> [Int] {
    let supoza1 = [1, 2, 3, 4, 5]
    let supoza2 = [2, 1, 2, 3, 2, 4, 2, 5]
    let supoza3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    var dict: [Int: Int] = [1: 0, 2: 0, 3: 0]
    
    for (index, answer) in answers.enumerated() {
        dict[1] = supoza1[index % 5] == answer ? dict[1]! + 1 : dict[1]
        dict[2] = supoza2[index % 8] == answer ? dict[2]! + 1 : dict[2]
        dict[3] = supoza3[index % 10] == answer ? dict[3]! + 1 : dict[3]
    }
    
    return dict.filter({ $0.value == dict.values.max() }).keys.sorted()
}
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