강의로 돌아가기
ggj0418

28번 테스트 케이스만 틀리는데 반례가 어떤게 있을까요..?

아래 방식으로 #이 붙은 코드들은 따로 다른 문자로 치환해서 풀었는데 안되더라구요... 질문 목록에 E#이 조건에는 없지만 테스트 케이스에서는 등장한다는 소문이 있어서 E#도 추가해봤는데 마찬가지로 28번 테스트 케이스만 안되더라구요...
도움 부탁드립니다 ㅜㅜ

  • playTime을 + 1 시켜줘도 봤는데 동일하게 28번만 안된다고 나옵니다
작성중인 코드―Solution.java
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
67
68
69
70
71
72
73
74
75
76
77
import java.util.*;

class Solution {
    public String solution(String m, String[] musicinfos) {
        String answer = "";
        Map<String, String> map = new HashMap<>();
        Map<String, String> answerMap = new HashMap<>();

        for(String musicInfo : musicinfos) {
            String[] splitArray = musicInfo.split(",");
            int playTime = getMusicTimeToInt(splitArray[1]) - getMusicTimeToInt(splitArray[0]);
            String b = splitArray[3];
            b = b.replaceAll("C#", "V");
            b = b.replaceAll("D#", "W");
            b = b.replaceAll("F#", "X");
            b = b.replaceAll("G#", "Y");
            b = b.replaceAll("A#", "X");
            map.put(splitArray[2], getMusicTable(playTime, b));
        }

        for(String key : map.keySet()) {
            if(isThatSong(m, map.get(key))) {
                answerMap.put(key, map.get(key));
            }
        }

        if(answerMap.size() == 0) return "(None)";

        List<String> keyList = new ArrayList<>(answerMap.keySet());
        Collections.sort(keyList, (o1, o2) -> answerMap.get(o2).length() - answerMap.get(o1).length());

        return keyList.get(0);
    }

    private boolean isThatSong(String key, String song) {
        key = key.replaceAll("C#", "V");
        key = key.replaceAll("D#", "W");
        key = key.replaceAll("F#", "X");
        key = key.replaceAll("G#", "Y");
        key = key.replaceAll("A#", "X");

        loop: for(int i = 0; i < song.length() - key.length() + 1; i++) {
            int kIndex = 0;
            for(int j = i; j < i + key.length(); j++) {
                if(key.charAt(kIndex) == song.charAt(j)) {
                    kIndex++;
                } else {
                    continue loop;
                }
            }
            return true;
        }

        return false;
    }

    private int getMusicTimeToInt(String time) {
        return Integer.parseInt(time.split(":")[0]) * 60 + Integer.parseInt(time.split(":")[1]);
    }

    private String getMusicTable(int time, String table) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < time; i++) {
            int index;
            if(i >= table.length()) {
                index = i % table.length();
            } else {
                index = i;
            }

            sb.append(table.charAt(index));
        }

        return sb.toString();
    }
}
1 개의 답변
이재왕

Map의 원소를 for문을 돌려 꺼내려하면 순서가 보장되지 않습니다.

이 문제의 중요한 조건 중 "하나가 재생길이가 같다면 앞에 나온 곡 이 정답이다."
즉, 순서가 보장되어야 합니다.

위의 코드에서 첫 부분을 아래와 같이 순서가 보장되는 LinkedHashMap으로 변경 후 실행하시면 통과됩니다.

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
LinkedHashMap<String, String> answerMap = new LinkedHashMap<String, String>();
  • ggj0418

    Map을 for문으로 돌려 꺼내려하면 순서가 보장되지 않는다는 건 처음 알았네요...! 정말 감사합니다 ㅠㅠ

    ggj0418―2021.07.05 15:16
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.