iOS 개발 공부
[Swift] 추억점수 본문
import Foundation
func solution1(_ name:[String], _ yearning:[Int], _ photo:[[String]]) -> [Int] {
let score: [String: Int] = Dictionary(uniqueKeysWithValues: zip(name, yearning))
return photo.map { $0.reduce(0) { $0 + (score[$1] ?? 0) } }
}
func solution2(_ name:[String], _ yearning:[Int], _ photo:[[String]]) -> [Int] {
return photo.map { $0.compactMap { name.firstIndex(of: $0) }.map { yearning[$0] }.reduce(0, +) }
}
'프로그래머스 > Lv.1' 카테고리의 다른 글
| [Swift] 덧칠하기 (0) | 2023.04.15 |
|---|---|
| [Swift] 카드뭉치 (0) | 2023.04.14 |
| [Swift] X만큼 간격이 있는 N개의 숫자 (0) | 2023.02.27 |
| [Swift] K번째 수 (0) | 2023.02.27 |
| [Swift] 행렬의 덧셈 (0) | 2023.02.27 |