CS/알고리즘_KAKAO BLIND RECRUITMENT

2018 KAKAO BLIND RECRUITMENT : [3차] 방금그곡

Jedy_Kim 2021. 11. 5. 21:52
728x90

https://programmers.co.kr/learn/courses/30/lessons/17683

 

코딩테스트 연습 - [3차] 방금그곡

방금그곡 라디오를 자주 듣는 네오는 라디오에서 방금 나왔던 음악이 무슨 음악인지 궁금해질 때가 많다. 그럴 때 네오는 다음 포털의 '방금그곡' 서비스를 이용하곤 한다. 방금그곡에서는 TV,

programmers.co.kr

 

// 코드1 (실패 -> 해결! 코드3--확인)

테스트 20 〉 실패 (12.59ms, 70.6MB)
테스트 21 〉 실패 (15.19ms, 81.4MB)
테스트 30 〉 실패 (27.56ms, 93.7MB)

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
66
import java.util.*;
 
class Solution {
    
    
    class MyMusic {    
        String title;
        int operTime;
        public MyMusic(String title, int operTime) {
            this.title    = title;
            this.operTime = operTime;
        }  
    }
    
    
    public String solution(String m, String[] musicinfos) {
        
        List<MyMusic> myList = new ArrayList<>();
        m = m.replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
 
        forString musicinfo : musicinfos ) {
            
            // 주어진 정보를 분할한다.
            String[] infos = musicinfo.split(",");
            
            // 시간을 계산 편의상 분으로 통합한다.   
            String[] startTimes = infos[0].split(":");                        
            String[] endTimes   = infos[1].split(":");
            int startTime       = (Integer.parseInt(startTimes[0]) * 60+ Integer.parseInt(startTimes[1]);
            int endTime         = (Integer.parseInt(endTimes[0]) * 60+ Integer.parseInt(endTimes[1]);
                        
            // 제목
            String title = infos[2];
            
            // 악보정보
            String contents = infos[3].replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
            
            // 00 : 00 - 23 : 00 : 음수이므로 절대값처리를 해준다.
            if(endTime == 0) endTime = 60 * 24;
            int operTime        = Math.abs(endTime - startTime);
            int orgOperTime     = operTime;
            
            // operTime 만큼 단어를 만든다. 
            String retStr = "";
            for ( int i=0; i<operTime; ++i)
                retStr += contents.charAt(i % contents.length()); 
            if( retStr.contains(m) ) myList.add(new MyMusic(title, orgOperTime));            
        }
        
        if( myList.size() == 0 ) return "(None)";
        
        if( myList.size() > 1) {
            // 조건에 맞는 음악이 여러개인 경우 재생시간이 긴곡, 재생시간도 같다면 먼저 입력된 곡
            Collections.sort(myList, new Comparator<MyMusic>() {           
                public int compare(MyMusic m1, MyMusic m2) {
                    if( m1.operTime == m2.operTime ) return 0;
                    if( m1.operTime > m2.operTime ) return 1;
                    else return -1;
                } 
            }); 
        }
        
         
        return myList.get(0).title;
    }
}
cs

 

// 코드2 (성공)

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
import java.util.*;
 
 
class Solution { 
    
    public String solution(String m, String[] musicinfos) {
        
        Arrays.sort(musicinfos, new Comparator<String>() {
           
            public int compare (String s1, String s2) {
                String[] infos      = s1.split(",");
                String[] startTimes = infos[0].split(":");                        
                String[] endTimes   = infos[1].split(":");
                int startTime       = (Integer.parseInt(startTimes[0]) * 60+ Integer.parseInt(startTimes[1]);
                int endTime         = (Integer.parseInt(endTimes[0]) * 60+ Integer.parseInt(endTimes[1]);
                int result1         = Math.abs(endTime - startTime);
                
                infos       = s2.split(",");
                startTimes  = infos[0].split(":");                        
                endTimes    = infos[1].split(":");
                startTime   = (Integer.parseInt(startTimes[0]) * 60+ Integer.parseInt(startTimes[1]);
                endTime     = (Integer.parseInt(endTimes[0]) * 60+ Integer.parseInt(endTimes[1]);
                int result2 = Math.abs(endTime - startTime);
                
                return result2 - result1;
            }
            
        });
         
        m = m.replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
 
        forString musicinfo : musicinfos ) {
            
            // 주어진 정보를 분할한다.
            String[] infos = musicinfo.split(",");
            
            // 시간을 계산 편의상 분으로 통합한다.   
            String[] startTimes = infos[0].split(":");                        
            String[] endTimes   = infos[1].split(":");
            int startTime       = (Integer.parseInt(startTimes[0]) * 60+ Integer.parseInt(startTimes[1]);
            int endTime         = (Integer.parseInt(endTimes[0]) * 60+ Integer.parseInt(endTimes[1]);
                        
            // 제목
            String title = infos[2];
            
            // 악보정보
            String contents = infos[3].replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
            
            // 00 : 00 - 23 : 00 : 음수이므로 절대값처리를 해준다. 
            int operTime = Math.abs(endTime - startTime);
            
            // operTime 만큼 단어를 만든다. 
            String retStr = "";
            for ( int i=0; i<operTime; ++i) retStr += contents.charAt(i % contents.length()); 
            
            if( retStr.contains(m) ) return title;
        }  
        
        return "(None)";
    }    
}
cs

 

// 코드3 통과

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
import java.util.*;
 
class Solution {
    
    
    class MyMusic {    
        String title;
        int operTime;
        public MyMusic(String title, int operTime) {
            this.title    = title;
            this.operTime = operTime;
        }  
    }
    
    
    public String solution(String m, String[] musicinfos) {
        
        List<MyMusic> myList = new ArrayList<>();
        m = m.replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
 
        forString musicinfo : musicinfos ) {
            
            // 주어진 정보를 분할한다.
            String[] infos = musicinfo.split(",");
            
            // 시간을 계산 편의상 분으로 통합한다.   
            String[] startTimes = infos[0].split(":");                        
            String[] endTimes   = infos[1].split(":");
            int startTime       = (Integer.parseInt(startTimes[0]) * 60+ Integer.parseInt(startTimes[1]);
            int endTime         = (Integer.parseInt(endTimes[0]) * 60+ Integer.parseInt(endTimes[1]);
                        
            // 제목
            String title = infos[2];
            
            // 악보정보
            String contents = infos[3].replace("C#""c").replace("D#""d").replace("F#""f").replace("A#""a").replace("G#""g");
            
            // 00 : 00 - 23 : 00 : 음수이므로 절대값처리를 해준다.
            int operTime        = Math.abs(endTime - startTime);
            int orgOperTime     = operTime;
            
            // operTime 만큼 단어를 만든다. 
            String retStr = "";
            for ( int i=0; i<operTime; ++i)
                retStr += contents.charAt(i % contents.length()); 
            if( retStr.contains(m) ) myList.add(new MyMusic(title, orgOperTime));            
        }
        
        if( myList.size() == 0 ) return "(None)";
        
        if( myList.size() > 1) {
            // 조건에 맞는 음악이 여러개인 경우 재생시간이 긴곡, 재생시간도 같다면 먼저 입력된 곡
            Collections.sort(myList, new Comparator<MyMusic>() {           
                public int compare(MyMusic m1, MyMusic m2) {
                    if( m1.operTime == m2.operTime ) return 0;
                    if( m1.operTime > m2.operTime ) return -1;
                    else return 1;
                } 
            }); 
        }
        
         
        return myList.get(0).title;
    }
}
cs

 

 

반응형