[JAVA 문제 풀이] 380. 방금그곡

프로그래머스 (17683)
Stupefyee's avatar
Jul 16, 2025
[JAVA 문제 풀이] 380. 방금그곡
notion image
 

내가 작성한 코드

class Solution { public String solution(String m, String[] musicinfos) { return getTitle(m, musicinfos); } // 주어진 멜로디 m이 재생된 음악 제목을 찾는 메소드 private String getTitle(String m, String[] musicinfos) { String findMelody = tranSemitone(m); // m의 멜로디를 반음들 소문자로 변환 String title = "(None)"; // 일치하는 음악 제목이 없을 경우 기본값 int playTime = Integer.MIN_VALUE; // 재생시간 비교를 위한 초기값 for (String musicinfo : musicinfos) { String[] parts = musicinfo.split(","); String start = parts[0]; // 음악 시작 시각 String end = parts[1]; // 음악 끝난 시각 String curTitle = parts[2]; // 음악 제목 String melody = tranSemitone(parts[3]); // 악보 정보 int duration = diffMinutes(start, end); // 음악 재생 시간 (분 단위) String fullMelody = getMelody(melody, duration); // 재생된 멜로디 // m의 멜로디가 재생된 멜로디에 포함되는지 확인 if (fullMelody.contains(findMelody) && fullMelody.length() > playTime) { title = curTitle; // 일치하는 음악 제목 업데이트 playTime = fullMelody.length(); // 현재 재생된 멜로디 길이 업데이트 } } // 일치하는 음악이 없을 경우 return title; } // 멜로디의 반음을 소문자로 변환 // C# → c, D# → d, F# → f, G# → g, A# → a, B# → b, E# → e private String tranSemitone(String melody) { return melody.replaceAll("C#", "c") .replaceAll("D#", "d") .replaceAll("F#", "f") .replaceAll("G#", "g") .replaceAll("A#", "a") .replaceAll("B#", "b") .replaceAll("E#", "e"); } // 시간을 분 단위로 변환 private int toMinutes(String time) { String[] parts = time.split(":"); int hour = Integer.parseInt(parts[0]); int minute = Integer.parseInt(parts[1]); return hour * 60 + minute; } // 시작 시각과 끝난 시각의 차이를 분 단위로 계산 private int diffMinutes(String start, String end) { return toMinutes(end) - toMinutes(start); } // 악보를 재생 시간에 맞게 반복하여 전체 멜로디를 생성 private String getMelody(String melody, int duration) { StringBuilder fullMelody = new StringBuilder(); int melodyLength = melody.length(); for (int i = 0; i < duration; i++) { fullMelody.append(melody.charAt(i % melodyLength)); } return fullMelody.toString(); } }
 

다른 사람의 코드

class Solution { public String solution(String m, String[] musicinfos) { String answer = "(None)"; int time = 0; // 재생 시간 초기화 m = edit(m); // 멜로디 편집 for (int inx = 0; inx < musicinfos.length; inx++) { String[] info = musicinfos[inx].split(","); // 음악 정보 분리 // 시작 시간과 종료 시간을 분으로 바꾼 뒤 차이 계산 int start = (60 * Integer.parseInt(info[0].substring(0, 2)) + Integer.parseInt(info[0].substring(3))); int end = (60 * Integer.parseInt(info[1].substring(0, 2)) + Integer.parseInt(info[1].substring(3))); int t = end - start; // 재생 시간이 현재 재생 시간보다 크면 if (t > time) { info[3] = edit(info[3]); // 멜로디 편집 StringBuffer sb = new StringBuffer(); // 재생 시간만큼 멜로디를 반복 for (int jnx = 0; jnx < t; jnx++) { sb.append(info[3].charAt(jnx % (info[3].length()))); } // 편집된 멜로디가 현재 멜로디를 포함하고 있는 경우 if (sb.toString().indexOf(m) >= 0) { answer = info[2]; time = t; } } } return answer; } public String edit(String m) { m = m.replaceAll("E#", "T"); m = m.replaceAll("B#", "U"); m = m.replaceAll("C#", "V"); m = m.replaceAll("D#", "W"); m = m.replaceAll("F#", "X"); m = m.replaceAll("G#", "Y"); m = m.replaceAll("A#", "Z"); return m; } }
 
Share article

stupefyee