|
| 1 | +package programmers.level2.week_24; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * [3차] 방금그곡 |
| 9 | + * https://programmers.co.kr/learn/courses/30/lessons/17683?language=java |
| 10 | + */ |
| 11 | +public class Solution003 { |
| 12 | + public static void main(String[] args) { |
| 13 | + Solution003 sol = new Solution003(); |
| 14 | + String m = "ABC"; |
| 15 | + String[] musicinfos = {"12:00,12:14,HELLO,C#DEFGAB", "13:00,13:05,WORLD,ABCDEF"}; |
| 16 | + System.out.println(sol.solution(m, musicinfos)); |
| 17 | + } |
| 18 | + |
| 19 | + public String solution(String m, String[] musicinfos) { |
| 20 | + String answer = ""; |
| 21 | + List<Music> answerList = new ArrayList<>(); |
| 22 | + String[] info; |
| 23 | + int minute1 = 0; |
| 24 | + int minute2 = 0; |
| 25 | + |
| 26 | + m = replaceStr(m); |
| 27 | + for (int i = 0; i < musicinfos.length; i++) { |
| 28 | + info = musicinfos[i].split(","); |
| 29 | + minute1 = (Integer.parseInt(info[0].substring(0, 2)) * 60) + (Integer.parseInt(info[0].substring(3, 5))); |
| 30 | + minute2 = (Integer.parseInt(info[1].substring(0, 2)) * 60) + (Integer.parseInt(info[1].substring(3, 5))); |
| 31 | + info[3] = replaceStr(info[3]); |
| 32 | + info[3] = info[3].repeat((minute2 - minute1) / info[3].length()) + info[3].substring(0, (minute2 - minute1) % info[3].length()); |
| 33 | + |
| 34 | + if (info[3].contains(m)) { |
| 35 | + answerList.add(new Music(info[2], minute2 - minute1)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (answerList.size() == 0) { |
| 40 | + return "(None)"; |
| 41 | + } |
| 42 | + |
| 43 | + Collections.sort(answerList); |
| 44 | + |
| 45 | + return answerList.get(0).getTitle(); |
| 46 | + } |
| 47 | + |
| 48 | + private String replaceStr(String s) { |
| 49 | + s = s.replaceAll("C#", "H"); |
| 50 | + s = s.replaceAll("D#", "I"); |
| 51 | + s = s.replaceAll("F#", "J"); |
| 52 | + s = s.replaceAll("G#", "K"); |
| 53 | + s = s.replaceAll("A#", "L"); |
| 54 | + |
| 55 | + return s; |
| 56 | + } |
| 57 | + |
| 58 | + class Music implements Comparable<Music> { |
| 59 | + String title; |
| 60 | + int playTime; |
| 61 | + |
| 62 | + public Music() { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public Music(String title, int playTime) { |
| 67 | + this.title = title; |
| 68 | + this.playTime = playTime; |
| 69 | + } |
| 70 | + |
| 71 | + public int getPlayTime() { |
| 72 | + return playTime; |
| 73 | + } |
| 74 | + |
| 75 | + public void setPlayTime(int playTime) { |
| 76 | + this.playTime = playTime; |
| 77 | + } |
| 78 | + |
| 79 | + public String getTitle() { |
| 80 | + return title; |
| 81 | + } |
| 82 | + |
| 83 | + public void setTitle(String title) { |
| 84 | + this.title = title; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public int compareTo(Music o) { |
| 89 | + return o.getPlayTime() - getPlayTime(); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments