프로그래밍/Algorithm

[프로그래머스] 완주하지 못한 선수

Jay Tech 2018. 10. 21. 19:34
반응형
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
public class UncompletedPlayer {
    public static void main(String[] args) {
        String[] participant = {"mislav""stanko""mislav""ana"};
        String[] completion = {"stanko""ana""mislav"};
        
        HashMap<String, Integer> map = new HashMap<String, Integer>();
 
        for(int i = 0; i < participant.length; i++) {
            if(map.containsKey(participant[i])) {
                map.put(participant[i], Integer.parseInt(map.get(completion[i]).toString()) + 1);
            } else {
                map.put(participant[i], 1);
            }
        }
        
        for(int i = 0; i < completion.length; i++) {
            if(map.containsKey(completion[i])) {
                map.put(completion[i], Integer.parseInt(map.get(completion[i]).toString()) - 1);
            } 
        }
 
        for(Map.Entry entry : map.entrySet()) {
            if(Integer.parseInt(entry.getValue().toString()) == 1) {
                System.out.println(entry.getKey());
            }
        }
       
        System.out.println(map);
    }
}
cs


해시 맵을 이용해서 했는데 테스트케이스 검사에서 타임아웃이 발생하였다.

처음에 vscode에서 하다가 이상하게 계속 타입 에러가 나서 막 꼬였었다. 문서를 뒤져봤는데 map에서 get을 때리면 return이 V라고 하는데 V는 Map<K,V>의 V였다. 그러면 내가 명시한 Integer로 되어야하는데 계속 object타입이라고 되지 않았었다. 


그리고 containsKey로 검사를 하지 않아도 됐었다.




다른 풀이


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
import java.util.HashMap;
import java.util.Map;
 
public class UncompletedPlayer_opt {
    public static void main(String[] args) {
        String[] participant = {"mislav""stanko""mislav""ana"};
        String[] completion = {"stanko""ana""mislav"};
        String answer = "";
 
        Map<String, Integer> hm = new HashMap<>();
 
        for(String part : participant) {
            if(hm.get(part) == null) {
                hm.put(part, 1);
            } else {
                hm.put(part, hm.get(part) + 1);
            }
        }
 
        for(String comp : completion) {
            hm.put(comp, hm.get(comp) - 1);
        }
 
        for(String key : hm.keySet()) {
            if(hm.get(key) == 1) answer = key;
        }
 
        System.out.println(answer);
    }
}
cs



트릭을 가진 풀이


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
 
public class UncompletedPlayer_array {
    public static void main(String[] args) {
        String[] participant = {"mislav""stanko""mislav""ana"};
        String[] completion = {"stanko""ana""mislav"};
        String answer = "";
 
        Arrays.sort(participant);
        Arrays.sort(completion);
        
        for(int i = 0; i < completion.length; i++) {
            if(!participant[i].equals(completion[i])) {
                answer = participant[i];
            }
        }
 
        System.out.println(answer);
    }
}
cs



completion 배열은 participant보다 1개 작다는 점을 교묘히 이용한 풀이이다. sort를 해도 시간제한에 걸리지 않았다.

신기신기...











반응형