문제 링크
https://programmers.co.kr/learn/courses/30/lessons/42576
문제 풀이
import java.util.HashMap;
import java.util.Map;
/**
* 완주하지 못한 선수
* https://programmers.co.kr/learn/courses/30/lessons/42576
*/
class Solution1 {
public String solution(String[] participant, String[] completion) {
Map<String, Integer> map = new HashMap<>();
// convert array to hashmap for completion
for (String person : participant) {
if (map.containsKey(person)) {
map.put(person, (int)map.get(person) + 1);
} else {
map.put(person, 1);
}
}
// find participant who completed
for(String person: completion) {
map.put(person, map.get(person) - 1);
}
String answer = "";
// search for participant who did not complete
for (String person: participant) {
if (map.get(person) != 0) {
answer = person;
break;
}
}
return answer;
}
}
반응형
'BE > algorithm' 카테고리의 다른 글
[LeetCode] 242. Valid Anagram (0) | 2022.12.14 |
---|---|
[LeetCode] 217. Contains Duplicate (2) | 2022.12.14 |
[프로그래머스] 다리를 지나는 트럭 - 스택/큐 (0) | 2021.07.18 |
[프로그래머스] K번째수 - 정렬 (0) | 2021.07.18 |
[프로그래머스] 모의고사 - 완전탐색 (0) | 2021.07.18 |