728x90
728x90
Swift μ€ν°λ 11μ£Όμ°¨ - ν΄μ :: μμ₯, λ² μ€νΈμ¨λ²
π 첫λ²μ§Έ νμ΄
# λμ λ리 νν
- playDict λ ["μ₯λ₯΄" : (μ΄ μ¬μ곑μ, [μ¬μ곑μ])] ννμ λμ λ리μ΄λ€.
- κ°μ μ₯λ₯΄μ μ΄ μ¬μ곑μμ λ°λΌ 1μ°¨ μ λ ¬μ ν΄μ€μΌνκΈ° λλ¬Έμ, μ΄ μ¬μ곑μλ₯Ό μΆκ°μ μΌλ‘ μ μ₯νλ€.
- musicDict λ ["μ₯λ₯΄_μ¬μ곑μ" : [κ³ μ λ²νΈ]] ννμ λμ λ리μ΄λ€.
- κ°μ μ₯λ₯΄μ κ°μ μ¬μ곑μλ₯Ό κ°μ§ λ Έλκ° μ¬λ¬κ° μμ μ μκΈ° λλ¬Έμ, κ³ μ λ²νΈλ₯Ό λ°°μ΄λ‘ μ μ₯νλ€.
playDict = {
"μ₯λ₯΄1" : {
μ΄ μ¬μ μ : 1300,
μ¬μ μ : [500, 500, 300]
},
"μ₯λ₯΄2" : { ... }
}
musicDict = {
"μ₯λ₯΄1_500" : [0, 1],
"μ₯λ₯΄1_300" : [2],
...
}
# λ² μ€νΈ μ¨λ²μ λͺ¨μλμλ λ€μ μμλ‘ λμνλ€.
- playDict μ μμλ€μ μ΄ μ¬μ곑μμ λ°λΌ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬νλ€.
- playDict μ κ° μμμ [μ¬μ곑μ] λ₯Ό λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬νλ€.
- μ κ³Όμ μ ν΅ν΄ musicDict μ ν€κ°μΈ "μ₯λ₯΄_μ¬μ곑μ" ννλ‘ λ§λ ν, [κ³ μ λ²νΈ]λ₯Ό μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ€.
- 2-3λ²μ λ°λ³΅νλ©° κ° μ₯λ₯΄λ§λ€ μ΅λ 2κ°μ λ Έλλ₯Ό λ² μ€νΈ μ¨λ²μ μΆκ°νλ€.
func solution(_ genres:[String], _ plays:[Int]) -> [Int] {
var playDict = [String: (total: Int, each: [Int])]()
var musicDict = [String: [Int]]()
for i in 0..<genres.count {
if playDict.keys.contains(genres[i]) {
playDict[genres[i]]!.total += plays[i]
playDict[genres[i]]!.each.append(plays[i])
} else {
playDict[genres[i]] = (total: plays[i], each: [plays[i]])
}
let key = genres[i] + String(plays[i])
if musicDict.keys.contains(key) {
musicDict[key]!.append(i)
} else {
musicDict[key] = [i]
}
}
var bestMusics = [Int]()
for (k, value) in playDict.sorted(by: { $0.value.total > $1.value.total }) {
var count = 0
for play in value.each.sorted(by: >) {
let key = k + String(play)
for music in musicDict[key]!.sorted() {
guard count < 2 else { break }
bestMusics.append(music)
count += 1
}
}
}
return bestMusics
}
π€ λλ²μ§Έ νμ΄
- 첫λ²μ§Έ νμ΄μμ playDict, musicDict λ κ°μ λμ λ리λ₯Ό μ°λ κ²μ νλλ‘ ν©μΉλ©΄ μ’κ² λ€ μΆμ΄, κ³ λ―Όν΄λ΄€λ€.
- JSON λ°μ΄ν°λ₯Ό λ°μλμ²λΌ λμ λ리 μμ λμ λ리 ννλ‘ κ³ λ €νλ€. (μλ μ°Έκ³ .. μ’ νλ Έμ μλ μλ€)
- κ·ΈμΉλ§ 2μ€ λμ λλ¦¬λ‘ κ΅¬ννλ €λ μμ μ κ·Ό λ°©λ²μ΄ λ무 볡μ‘νκ³ , 첫λ²μ§Έ νμ΄λ³΄λ€ μκ°μ΄ μ’ λ κ±Έλ Έλ€.
# λμ λ리 νν
- λ¨Όμ , Play λ κ° μ₯λ₯΄λ§λ€ νμν κ°λ€μ μ μ₯νλ (μ΄ μ¬μμ, [μ¬μμ: [κ³ μ λ²νΈ]]) ννμ ννμ΄λ€.
- musics λ [μ₯λ₯΄: Play] ννμ λμ λ리μ΄λ€.
- νλ²μ νμΈν΄λ³΄λ©΄ musics λ [μ₯λ₯΄:(μ΄ μ¬μμ, [μ¬μμ: [κ³ μ λ²νΈ]])] μΈ κ²μ΄λ€.
musics = {
"μ₯λ₯΄1" :
{
"μ΄ μ¬μμ" : 1300
"κ° μ¬μμ" : [
{ 300 : [0] },
{ 500 : [1, 2] }
]
}
"μ₯λ₯΄2" : { ... }
}
# λ² μ€νΈ μ¨λ²μ λͺ¨μλμλ λ€μ μμλ‘ λμνλ€.
- musics λμ λ리λ₯Ό μ 체 μ¬μ μμ λ°λΌ λ΄λ¦Όμ°¨μ μ λ ¬νλ€.
- musics λμ λ리 μμμ value μΈ (total: μ 체μ¬μμ, each: [μ¬μ μ: [κ³ μ λ²νΈ]]) μ΄λ€.
- value.each μΈ [μ¬μ μ: [κ³ μ λ²νΈ]] λ₯Ό μ¬μ μμ λ°λΌ λ΄λ¦Όμ°¨μ μ λ ¬νλ€.
- [κ³ μ λ²νΈ] λ₯Ό μ€λ¦μ°¨μ μ λ ¬ν΄μ, bestMusics λ°°μ΄μ μ΅λ 2κ°λ§νΌ λ£μ΄μ€λ€.
func solution(_ genres:[String], _ plays:[Int]) -> [Int] {
// [μ₯λ₯΄: (μ 체μ¬μμ, [μ¬μμ:[κ³ μ λ²νΈ]])]
typealias Play = (total: Int, ids: [Int:[Int]])
var musics = [String:Play]()
for i in 0..<genres.count {
let key = genres[i], play = plays[i]
if musics.keys.contains(key) {
musics[key]!.total += play
if musics[key]!.ids.keys.contains(play) {
musics[key]!.ids[play]!.append(i)
} else {
musics[key]!.ids[play] = [i]
}
} else {
musics[key] = Play(total: play, ids: [play: [i]])
}
}
var bestMusics = [Int]()
for (_, value) in musics.sorted(by: { $0.value.total > $1.value.total }) {
var count = 0
for (_, ids) in value.ids.sorted(by: {$0.key > $1.key}) {
for id in ids {
guard count < 2 else { break }
bestMusics.append(id)
count += 1
}
}
}
return bestMusics
}
Review :: μμ²λ μκ°μ°¨μ΄κ° λλ 건 μλμ§λ§, νμ΄1κ³Ό νμ΄2μ€μ μ΄λ€κ² λ 보기μ’μ νμ΄μΈμ§ ꡬλΆνκΈ°κ° μ΄λ ΅λ€..
728x90
728x90