Closed
Description
💬 문제
[코딩테스트 연습 - 모의고사](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()
}