๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

2018 KAKAO BLIND RECRUITMENT[3์ฐจ] ๋ฐฉ๊ธˆ๊ทธ๊ณก Swift

by Fomagran ๐Ÿ’ป 2020. 11. 29.
728x90
๋ฐ˜์‘ํ˜•

Source Code


import Foundation
////#์ด ํฌํ•จ๋œ ์Œ ์†Œ๋ฌธ์ž๋กœ ๊ณ ์น˜๋Š” ํ•จ์ˆ˜
func replaceString(_ melody:String) -> String{
var replaceString = melody
//replaceOccurrences๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž๋ฅผ ๋Œ€์ฒดํ•ด์ค€๋‹ค.
replaceString = replaceString.replacingOccurrences(of: "C#", with: "c")
replaceString = replaceString.replacingOccurrences(of: "D#", with: "d")
replaceString = replaceString.replacingOccurrences(of: "F#", with: "f")
replaceString = replaceString.replacingOccurrences(of: "G#", with: "g")
replaceString = replaceString.replacingOccurrences(of: "A#", with: "a")
return replaceString
}
func solution(_ m:String, _ musicinfos:[String]) -> String {
//ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„ ์ค‘ ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์„ ๋‹ด์„ ๋ณ€์ˆ˜
var maxPlayTime = Int()
//์ •๋‹ต์ธ ๋…ธ๋ž˜ ์ œ๋ชฉ์„ ๋‹ด์„ ๋ณ€์ˆ˜
var answer = String()
//์Œ์•…์ด ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
var musicEnd = Int()
//ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋“ค์–ด์˜จ m์„ ๋Œ€์ฒดํ•œ ๋ณ€์ˆ˜
let replaceM = replaceString(m)
//for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฎค์ง์ธํฌ๋ฅผ ํƒ์ƒ‰ํ•ด์ค€๋‹ค.
for i in 0..<musicinfos.count {
//split์„ ์‚ฌ์šฉํ•˜์—ฌ , ๋‹จ์œ„๋กœ ๋Š์–ด์คŒ
let music = musicinfos[i].split(separator: ",")
/*์Œ์•…์ด ์‹œ์ž‘ํ•˜๋Š” ์‹œ๊ฐ„์€ music์•ˆ์—์„œ 0๋ฒˆ์จฐ์ด๋ฏ€๋กœ prefix๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์—์„œ ๋‘๋ฒˆ์งธ๋ฅผ ์•Œ์•„๋‚ด๊ณ  ์ด๊ฒƒ์€ ์‹œ๊ฐ„์ด๋ฏ€๋กœ * 60์„ ํ•ด์ค€๋‹ค
๋˜ํ•œ ๋์—์„œ ๋‘๋ฒˆ์งธ๋Š” ๋ถ„์ด๋ฏ€๋กœ ๋”ํ•ด์ค€๋‹ค.
*/
let musicStart = Int(music[0].prefix(2))!*60 + Int(music[0].suffix(2))!
//๋งŒ์•ฝ 23์‹œ - 00์‹œ๊ฐ€ ๋˜๋ฉด ๋ฎค์ง ์Šคํƒ€ํŠธ๊ฐ€ ๋ฎค์ง ์—”๋“œ๋ณด๋‹ค ์‹œ๊ฐ„์ด ๋” ๋‚ฎ์•„์ง€๋ฏ€๋กœ ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์–ด์ค€๋‹ค.
if Int(music[0].prefix(2))! > Int(music[1].prefix(2))! {
//๋ฎค์ง์—”๋“œ๋„ ๋ฎค์ง์Šคํƒ€ํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด์ค€๋‹ค. ๋‹ค๋งŒ ์ด๊ฒƒ์€ 00์‹œ๊ฐ€ ๋œ๊ฒƒ์ด๋ฏ€๋กœ ์•ž์— ์‹œ๊ฐ„์— 24๋ฅผ ๊ณฑํ•ด์ค€๋‹ค.
musicEnd = (Int(music[1].prefix(2))!+24)*60 + Int(music[1].suffix(2))!
}else{
musicEnd = Int(music[1].prefix(2))!*60 + Int(music[1].suffix(2))!
}
//์Œ์•…์ด ๋๋‚˜๋Š” ์‹œ๊ฐ„์—์„œ ์Œ์•…์ด ์‹œ์ž‘ํ•˜๋Š” ์‹œ๊ฐ„์„ ๋นผ์ฃผ์–ด์„œ ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„์„ ์ •ํ•ด์คŒ
let musicPlayTime = musicEnd - musicStart
//๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์ด ํ˜„์žฌ ์Œ์•…ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ์˜๋ฏธ์—†์œผ๋ฏ€๋กœ ์ปจํ‹ฐ๋‰ด๋กœ ํŒจ์Šคํ•ด์ค€๋‹ค.
if maxPlayTime >= musicPlayTime{
continue
}
//music์˜ 3๋ฒˆ์งธ ์ธ๋ฑ์Šค๋Š” ๋ฉœ๋กœ๋””๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ ๊ณ ๋กœ ์ด๊ฒƒ๋„ ์•Œ๋งž๊ฒŒ ๋Œ€์ฒดํ•ด์ค€๋‹ค.
var replaceMusic = replaceString("\(music[3])")
//๋งŒ์•ฝ ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„์ด ๋ฉœ๋กœ๋””๊ฐ€ ์ €์žฅ๋œ ์‹œ๊ฐ„๋ณด๋‹ค ๊ธธ๋‹ค๋ฉด
if musicPlayTime > replaceMusic.count {
//ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„์„ ๋ฉœ๋กœ๋”” ์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„์–ด์ฃผ๊ณ  -1์„ ํ•ด์ฃผ๋ฉด ๋ช‡๋ฒˆ ๋” ๋ฐ˜๋ณต๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
let rep = musicPlayTime/replaceMusic.count - 1
//ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„์„ ๋ฉœ๋กœ๋”” ์‹œ๊ฐ„์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค€ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•ด์คŒ
let left = musicPlayTime%replaceMusic.count
//๋ฐ˜๋ณต๋œ๋งŒํผ replaceMusic์— ๋”ํ•ด์คŒ
for _ in 0..<rep {
replaceMusic += String(replaceString("\(music[3])"))
}
//๋‚˜๋จธ์ง€๋งŒํผ prefix๋กœ ์ž˜๋ผ์„œ ๋งˆ์ € ๋”ํ•ด์ค€๋‹ค.
replaceMusic += replaceMusic.prefix(left)
//๋งŒ์•ฝ ํ”Œ๋ ˆ์ด ์‹œ๊ฐ„์ด ๋ฉœ๋กœ๋””๊ฐ€ ์ €์žฅ๋œ ์‹œ๊ฐ„๋ณด๋‹ค ์งง๋‹ค๋ฉด
}else{
//๊ทธ๋งŒํผ ์งค๋ผ์„œ replaceMusic์„ ๋ฐ”๊ฟ”์คŒ
replaceMusic = String(replaceString("\(music[3])").prefix(musicPlayTime))
}
//์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ replaceMusic์— replaceM์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด
if replaceMusic.contains(replaceM) {
//์ •๋‹ต์˜ ์ œ๋ชฉ์„ ๋ฐ”๊ฟ”์คŒ
answer = "\(music[2])"
//๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„ ๋˜ํ•œ ๋ฐ”๊ฟ”์คŒ
maxPlayTime = musicPlayTime
}
}
//์ •๋‹ต์ด ๋น„์–ด์žˆ๋‹ค๋ฉด (None)์„ ์•„๋‹ˆ๋ผ๋ฉด answer๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คŒ
return answer == "" ? "(None)" : answer
}
solution("ABC", ["00:01,00:08,HI,ABC#ABC"])

Solution


ํ•ต์‹ฌ

#์ด ๋“ค์–ด๊ฐ„ ๋ฌธ์ž๋ฅผ replaceOccurrences๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ฒดํ•ด์ค„ ๊ฒƒ

 

23์‹œ์—์„œ 00์‹œ๋กœ ๋ณ€ํ•  ๋•Œ์— ์ž˜ ๋งž๊ฒŒ ํ•ด์ค„ ๊ฒƒ

 

ํ”Œ๋ ˆ์ด๋œ ์‹œ๊ฐ„๋งŒํผ ๋ฉœ๋กœ๋””๋ฅผ ๋ฐ˜๋ณตํ•ด์ฃผ๊ณ  prefix๋ฅผ ์ด์šฉํ•ด ์ž˜๋ผ์ค„ ๊ฒƒ

 

Problem


 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [3์ฐจ] ๋ฐฉ๊ธˆ๊ทธ๊ณก

๋ฐฉ๊ธˆ๊ทธ๊ณก ๋ผ๋””์˜ค๋ฅผ ์ž์ฃผ ๋“ฃ๋Š” ๋„ค์˜ค๋Š” ๋ผ๋””์˜ค์—์„œ ๋ฐฉ๊ธˆ ๋‚˜์™”๋˜ ์Œ์•…์ด ๋ฌด์Šจ ์Œ์•…์ธ์ง€ ๊ถ๊ธˆํ•ด์งˆ ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ๊ทธ๋Ÿด ๋•Œ ๋„ค์˜ค๋Š” ๋‹ค์Œ ํฌํ„ธ์˜ '๋ฐฉ๊ธˆ๊ทธ๊ณก' ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๊ณค ํ•œ๋‹ค. ๋ฐฉ๊ธˆ๊ทธ๊ณก์—์„œ๋Š” TV,

programmers.co.kr

 

30๋ฒˆ ํ•˜๋‚˜๋งŒ ํ‹€๋ ธ๋˜ ํ’€์ด

 

์•„๋ž˜๋ฅผ ๋ณด๋ฉด replaceOccurrences๋ฅผ ๋ชฐ๋ž๋˜ ์ƒํƒœ์—์„œ ํ’€์—ˆ๋˜ ๊ฒƒ์ด๋‹ค.

 

์ด๊ฒƒ์€ ๊ฒฐ๊ตญ m์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ #์ด ํฌํ•จ๋œ ๋ฌธ์ž์ธ์ง€ ์•„๋‹Œ์ง€๋งŒ ์•Œ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹ˆ์•ผ? ๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ 

 

m์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์ธ ๊ฒƒ๋งŒ ๋ณ€ํ˜•ํ•ด์„œ ํ’€์–ด์ฃผ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ณ„์† 30๋ฒˆ์ด ํ‹€๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ด๊ฒƒ์€ ํ”Œ๋ ˆ์ด๋œ ์‹œ๊ฐ„์˜ ์Œ๋ณด๋‹ค m์˜ ๊ธธ์ด๊ฐ€ ๋” ๊ธธ ๊ฒฝ์šฐ๋ฅผ ์บ์น˜ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค๋‹ค.

 

ex) m = "CBA#D#F#C"  , melody = "CBA#D#F#" ์ผ ๊ฒฝ์šฐ ๊ธธ์ด๋ฅผ ์žฌ๊ธฐ๊ฐ€ ํž˜๋“ค์–ด์ง.

 

์–ด๋–ป๊ฒŒ๋“  ํ’€๋ฉด ํ’€์—ˆ์„๊ฑฐ ๊ฐ™์€๋ฐ ํšจ์œจ์ ์ด์ง€ ์•Š๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Foundation
 
func solution(_ m:String, _ musicinfos:[String]) -> String {
 
    var maxPlayTime = Int()
    var answer = String()
    var musicEnd = Int()
    for i in 0..<musicinfos.count {
        let music = musicinfos[i].split(separator: ",")
        let musicStart = Int(music[0].prefix(2))!*60 + Int(music[0].suffix(2))!
        if Int(music[0].prefix(2))! > Int(music[1].prefix(2))! {
            musicEnd = (Int(music[1].prefix(2))!+24)*60 + Int(music[1].suffix(2))!
        }else{
        musicEnd = Int(music[1].prefix(2))!*60 + Int(music[1].suffix(2))!
        }
        
        let musicPlayTime = musicEnd - musicStart
        
        if maxPlayTime >= musicPlayTime {
            continue
        }
        var melody = music[3]
        
        if music[3].count < m.count {
            let rep = m.count/music[3].count
            for _ in 0...rep {
                melody += music[3]
            }
        }else{
            melody += music[3]
        }
        
        var melodyMap = melody.map{String($0)}
        var mMap = m.map{String($0)}
        
        for i in 1..<melodyMap.count {
            if melodyMap[i] == "#"  && melodyMap[i-1== String(m.last!){
                melodyMap[i-1= "Z"
            }
        }
        
        for i in 1..<mMap.count {
            if mMap[i] == "#"  && mMap[i-1== String(m.last!){
                mMap[i-1= "Z"
            }
        }
        
        if music[3].count == m.count && m.first! == "#" {
            mMap.removeLast()
            mMap.append("Z")
        }
        
        if melodyMap.joined().contains(mMap.joined()) {
            answer = "\(music[2])"
            maxPlayTime = musicPlayTime
        }
    }
    
    return answer == "" ?  "(None)" : answer
}

 

์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฒƒ

 

replaceOccurrences

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€