-
Notifications
You must be signed in to change notification settings - Fork 0
방금그곡 #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
방금그곡 #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||||||||||||||||||||||
| // 프로그래머스 - 방금그곡 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func solution(_ m: String, _ musicinfos: [String]) -> String { | ||||||||||||||||||||||||||||||||||
| var selected: (String, Int) = ("(None)", 0) | ||||||||||||||||||||||||||||||||||
| let heard = convertSheet(m) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| for music in musicinfos { | ||||||||||||||||||||||||||||||||||
| let info = music.split(separator: ",").map { String($0) } | ||||||||||||||||||||||||||||||||||
| guard info.count == 4 else { continue } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| let duration = toNumber(info[1]) - toNumber(info[0]) | ||||||||||||||||||||||||||||||||||
| let sheet = convertSheet(info[3]) | ||||||||||||||||||||||||||||||||||
| guard !sheet.isEmpty, duration > 0 else { continue } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| let repeatCount = duration / sheet.count + 1 | ||||||||||||||||||||||||||||||||||
| let played = String((String(repeating: sheet, count: repeatCount)).prefix(duration)) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if played.contains(heard) && duration > selected.1 { | ||||||||||||||||||||||||||||||||||
| selected = (info[2], duration) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return selected.0 | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func toNumber(_ str: String) -> Int { | ||||||||||||||||||||||||||||||||||
| let sections = str.split(separator: ":").map { String($0) } | ||||||||||||||||||||||||||||||||||
| guard sections.count == 2, | ||||||||||||||||||||||||||||||||||
| let min = Int(sections[0]), | ||||||||||||||||||||||||||||||||||
| let sec = Int(sections[1]) | ||||||||||||||||||||||||||||||||||
| else { return 0 } | ||||||||||||||||||||||||||||||||||
| return min * 60 + sec | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func convertSheet(_ str: String) -> String { | ||||||||||||||||||||||||||||||||||
| var sheet: String = str | ||||||||||||||||||||||||||||||||||
| while let point = sheet.firstIndex(of: "#") { | ||||||||||||||||||||||||||||||||||
| let target = sheet.index(before: point) | ||||||||||||||||||||||||||||||||||
| let note = sheet[target].lowercased() | ||||||||||||||||||||||||||||||||||
| sheet.removeSubrange(target...point) | ||||||||||||||||||||||||||||||||||
| sheet.insert(contentsOf: note, at: target) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return sheet | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+35
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
func convertSheet(_ str: String) -> String {
return str
.replacingOccurrences(of: "C#", with: "c")
.replacingOccurrences(of: "D#", with: "d")
.replacingOccurrences(of: "F#", with: "f")
.replacingOccurrences(of: "G#", with: "g")
.replacingOccurrences(of: "A#", with: "a")
} |
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재생 시간(
duration) 만큼의 전체 멜로디를 생성하는 현재 방식은 매우 비효율적일 수 있습니다.String(repeating:count:)으로 악보를duration / sheet.count + 1만큼 반복하여 매우 긴 중간 문자열을 생성한 후,prefix(duration)으로 잘라내고 있습니다. 예를 들어, 재생 시간이 1400분이고 악보 길이가 2라면, 약 1400 * 701 = 981400 길이의 문자열을 생성하게 됩니다. 이는 불필요한 메모리 할당과 연산을 유발합니다. 필요한 길이만큼만 멜로디를 생성하는 것이 훨씬 효율적입니다.