λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
🐍 Algorithm/μ•Œκ³ λ¦¬μ¦˜-Swift

[μ•Œκ³ λ¦¬μ¦˜/Swift] ν•΄μ‹œ - λ² μŠ€νŠΈμ•¨λ²”

by Danna 2021. 7. 6.
728x90
728x90

Swift μŠ€ν„°λ”” 11μ£Όμ°¨ - ν•΄μ‹œ :: μœ„μž₯, λ² μŠ€νŠΈμ•¨λ²”

 

[μ•Œκ³ λ¦¬μ¦˜/Swift] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - μœ„μž₯ (ν•΄μ‹œ)

Python3 둜 ν’€μ—ˆλ˜ 문제λ₯Ό Swift 둜 λ‹€μ‹œ ν’€μ–΄λ΄€μ–΄μš”. iOS κ°œλ°œμ„ 쒀더 효율적으둜 ν•˜κΈ°μœ„ν•΄μ„œ Swift 데이터ꡬ쑰, μ•Œκ³ λ¦¬μ¦˜ κ³΅λΆ€μ˜ ν•„μš”μ„±μ„ λŠκΌˆμŠ΅λ‹ˆλ‹€. πŸ€” 'μœ„μž₯' λ¬Έμ œμ— λŒ€ν•œ μ ‘κ·Ό 방법은 이전 포

jellysong.tistory.com

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ ν•΄μ‹œ - λ² μŠ€νŠΈμ•¨λ²”

πŸ‘€ 첫번째 풀이

# λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœ

  • playDict λŠ” ["μž₯λ₯΄" : (총 μž¬μƒκ³‘μˆ˜, [μž¬μƒκ³‘μˆ˜])] ν˜•νƒœμ˜ λ”•μ…”λ„ˆλ¦¬μ΄λ‹€.
  • 같은 μž₯λ₯΄μ˜ 총 μž¬μƒκ³‘μˆ˜μ— 따라 1μ°¨ 정렬을 ν•΄μ€˜μ•Όν•˜κΈ° λ•Œλ¬Έμ—, 총 μž¬μƒκ³‘μˆ˜λ₯Ό μΆ”κ°€μ μœΌλ‘œ μ €μž₯ν–ˆλ‹€.
  • musicDict λŠ” ["μž₯λ₯΄_μž¬μƒκ³‘μˆ˜" : [고유번호]] ν˜•νƒœμ˜ λ”•μ…”λ„ˆλ¦¬μ΄λ‹€.
  • 같은 μž₯λ₯΄μ— 같은 μž¬μƒκ³‘μˆ˜λ₯Ό 가진 λ…Έλž˜κ°€ μ—¬λŸ¬κ°œ μžˆμ„ 수 있기 λ•Œλ¬Έμ—, 고유번호λ₯Ό λ°°μ—΄λ‘œ μ €μž₯ν–ˆλ‹€.
playDict = {
  "μž₯λ₯΄1" : {
    총 μž¬μƒ 수 : 1300,
    μž¬μƒ 수 : [500, 500, 300]
  },
  "μž₯λ₯΄2" : { ... }
}

musicDict = {
  "μž₯λ₯΄1_500" : [0, 1],
  "μž₯λ₯΄1_300" : [2],
  ...
}

 

# 베슀트 앨범을 λͺ¨μ„λ•Œμ—λŠ” λ‹€μŒ μˆœμ„œλ‘œ λ™μž‘ν•œλ‹€.

  1. playDict 의 μš”μ†Œλ“€μ„ μ΄ μž¬μƒκ³‘μˆ˜μ— 따라 λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  2. playDict 의 각 μš”μ†Œμ˜ [μž¬μƒκ³‘μˆ˜] λ₯Ό λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  3. μœ„ 과정을 톡해 musicDict 의 킀값인 "μž₯λ₯΄_μž¬μƒκ³‘μˆ˜" ν˜•νƒœλ‘œ λ§Œλ“  ν›„, [고유 번호]λ₯Ό μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  4. 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" : { ... }
}

 

# 베슀트 앨범을 λͺ¨μ„λ•Œμ—λŠ” λ‹€μŒ μˆœμ„œλ‘œ λ™μž‘ν•œλ‹€.

  1. musics λ”•μ…”λ„ˆλ¦¬λ₯Ό 전체 μž¬μƒ μˆ˜μ— 따라 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•œλ‹€.
  2. musics λ”•μ…”λ„ˆλ¦¬ μš”μ†Œμ˜ value 인 (total: μ „μ²΄μž¬μƒμˆ˜, each: [μž¬μƒ 수: [고유번호]]) 이닀.
  3. value.each 인 [μž¬μƒ 수: [고유번호]] λ₯Ό μž¬μƒ μˆ˜μ— 따라 λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•œλ‹€.
  4. [고유번호] λ₯Ό μ˜€λ¦„μ°¨μˆœ μ •λ ¬ν•΄μ„œ, 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쀑에 μ–΄λ–€κ²Œ 더 보기쒋은 풀이인지 κ΅¬λΆ„ν•˜κΈ°κ°€ μ–΄λ ΅λ‹€.. 

첫번째 풀이 vs λ‘λ²ˆμ§Έ 풀이 

728x90
728x90