728x90
https://programmers.co.kr/learn/courses/30/lessons/17683
// 코드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");
for( String 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");
for( String 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");
for( String 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 |
반응형
'CS > 알고리즘_KAKAO BLIND RECRUITMENT' 카테고리의 다른 글
2018 KAKAO BLIND RECRUITMENT : [1차] 프렌즈4블록 (0) | 2021.11.08 |
---|---|
2020 KAKAO BLIND RECRUITMENT : 문자열 압축 (0) | 2021.11.04 |
2018 KAKAO BLIND RECRUITMENT : [3차] 파일명 정렬 (0) | 2021.11.03 |
2019 KAKAO BLIND RECRUITMENT : 블록 게임 (0) | 2021.11.02 |
2018 KAKAO BLIND RECRUITMENT : [3차] n진수 게임 (0) | 2021.11.01 |