단순하게는 작성하지 못했지만..
나름대로 만족.....
let n = answers.count
var arr1 = [1,2,3,4,5]
var arr2 = [2,1,2,3,2,4,2,5]
var arr3 = [3,3,1,1,2,2,4,4,5,5]
var dict = [1 : 0, 2 : 0, 3 : 0]
for i in 0..<answers.count {
if answers[i] == arr1[i%5] {
dict[1]! += 1
}
if answers[i] == arr2[i%8] {
dict[2]! += 1
}
if answers[i] == arr3[i%10] {
dict[3]! += 1
}
}
var max = dict.values.max()!
for i in 1...3 {
if dict[i] != max {
dict[i] = nil
}
}
var sortedDict = dict.sorted {
if $0.1 == $1.1 {
return $0 < $1
} else {
return $0.1 > $1.1
}
}
var result = [Int]()
for i in sortedDict {
result.append(i.key)
}
print(result)
프린트만 리턴으로 바꿔주면됨
'알고리즘문풀 with SWIFT > Programmers' 카테고리의 다른 글
swift ) 프로그래머스 - 행렬의 덧셈 (0) | 2022.05.23 |
---|---|
swift ) 프로그래머스 - x만큼 간격이 있는 n개의 숫자 (0) | 2022.05.23 |
swift ) 프로그래머스 - 직사각형 별찍기 (0) | 2022.05.23 |
swift ) 프로그래머스 - 체육복 (0) | 2022.05.07 |
swift ) 프로그래머스 - 카카오 2019 블채 레벨1 - 실패율 (0) | 2022.05.06 |