Skip to content

Commit 449a1b3

Browse files
committed
[PGS] 완주하지 못한 선수 (Java) / Level 1 / 15분 / 성공
1 parent 7aee3c3 commit 449a1b3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 풀이 1: 정렬을 이용한 풀이
2+
import java.util.*;
3+
4+
class Solution {
5+
public String solution(String[] participant, String[] completion) {
6+
Arrays.sort(participant);
7+
Arrays.sort(completion);
8+
9+
for (int i = 0; i < completion.length; i++) {
10+
if (!participant[i].equals(completion[i])) {
11+
return participant[i];
12+
}
13+
}
14+
15+
return participant[participant.length - 1];
16+
}
17+
}
18+
19+
// 풀이2: 해시맵을 이용한 풀이
20+
import java.util.*;
21+
22+
class Solution {
23+
public String solution(String[] participant, String[] completion) {
24+
String answer = "";
25+
26+
HashMap<String, Integer> dict = new HashMap<>();
27+
28+
for (String p : participant) dict.put(p, dict.getOrDefault(p, 0) + 1);
29+
for (String c : completion) dict.put(c, dict.get(c) - 1);
30+
31+
for (String key: dict.keySet()) {
32+
if (dict.get(key) != 0) {
33+
answer = key;
34+
}
35+
}
36+
37+
return answer;
38+
}
39+
}

0 commit comments

Comments
 (0)