본문 바로가기
Problem Solving

[프로그래머스] L2 방금그곡 / 2018 카카오 블라인드 채용 (Java)

by JYHAN 2020. 12. 19.

카카오

[프로그래머스] L2 방금그곡 / 2018 카카오 블라인드 채용

[풀이]

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
61
62
63
64
65
class Solution {
    public String solution(String m, String[] musicinfos) {
        String answer = "";
        int[] timeList = new int[musicinfos.length]; // i번째 곡의 플레이 시간
        String[] melodyList = new String[musicinfos.length]; // i번째 곡의 곡정보
        for(int i=0; i<musicinfos.length; i++){
            // info: 시작 시각, 끝난 시각, 제목, 악보
            String[] info = musicinfos[i].split(",");
            String[] sTime = info[0].split(":");
            String[] eTime = info[1].split(":");
            int st = Integer.parseInt(sTime[0])*60 + Integer.parseInt(sTime[1]);
            int et = Integer.parseInt(eTime[0])*60 + Integer.parseInt(eTime[1]);
            int etst = et - st;
            
            String melody = "";
            String um = info[3];
            int idx = 0;
            for(int time=0; time<etst; time++){
                if(idx==um.length()) idx = 0;
                melody += Character.toString(um.charAt(idx));
                idx++;
                if(idx==um.length()) idx = 0;
                if(um.charAt(idx)=='#'){
                    melody += Character.toString(um.charAt(idx));
                    idx++;
                }
            }
 
            timeList[i] = etst;
            melodyList[i] = melody;
        }
        
        int idx = -1;
        int len = -1;
        for(int i=0; i<timeList.length; i++){
            int time = timeList[i];
            String melody = melodyList[i];
            if(melody.contains(m)){
                boolean isDone = false;
                int index = melody.indexOf(m); // 해당 문자의 시작 위치
                do {
                    int lastIndex = index+m.length();
                    if(lastIndex==melody.length()) {
                        isDone = true;
                        break;
                    }else {
                        if(melody.charAt(lastIndex)!='#') {
                            isDone = true;
                            break;
                        }
                    }
                    index = melody.indexOf(m, lastIndex);
                }while(index!=-1);
                
                if(isDone && len<time){
                    len = time;
                    idx = i;
                }
            }
        }
        if(idx==-1) answer = "(None)";
        else answer = musicinfos[idx].split(",")[2];
        return answer;
    }
}
cs

댓글