Algorithm/Algorithm Practice
프로그래머스 -7 완주하지 못한 선수 (Java)
GrapeMilk
2020. 3. 27. 00:35
문제 링크 ( https://programmers.co.kr/learn/courses/30/lessons/42576 )
- 해시를 이용한 풀이
import java.util.HashMap;
import java.util.Map;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
int val = 0;
Map<String, Integer> hm = new HashMap<>();
for (String part : participant) {
if (hm.get(part) == null) {
hm.put(part, 1);
}
else {
val = hm.get(part) + 1;
hm.put(part, val);
}
}
for (String comp : completion) {
val = hm.get(comp) - 1;
hm.put(comp, val);
}
for (String key : hm.keySet()) {
if (hm.get(key) == 1) answer = key;
}
return answer;
}
}
*필요한 개념
- HashMap 생성 방법
- HashMap 주요 메서드 (get, keySet, put)
*HashMap 개념 참고
( https://vaert.tistory.com/107 )
*코드 참조