diff --git "a/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" "b/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" index 23d7824..172f2b2 100644 --- "a/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" +++ "b/week2/1. \354\231\204\354\243\274\355\225\230\354\247\200 \353\252\273\355\225\234 \354\204\240\354\210\230/Solution.java" @@ -1,7 +1,60 @@ +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + /* 1. 완주하지 못한 선수 https://programmers.co.kr/learn/courses/30/lessons/42576 */ public class Solution { + public static void main(String[] args) { + + String[] participant = { "leo", "kiki", "eden" }; + String[] completion = { "eden", "kiki" }; + + System.out.println(solution(participant, completion)); + System.out.println(solution2(participant, completion)); + } + + public static String solution(String[] participant, String[] completion) { + + Arrays.sort(participant); + Arrays.sort(completion); + + for (int i = 0; i < completion.length; i++) { + if (!participant[i].equals(completion[i])) { + return participant[i]; + } + } + + return participant[participant.length - 1]; + } + + public static String solution2(String[] participant, String[] completion) { + + Map checkerMap = new HashMap<>(); + + for (String person : participant) { + checkerMap.put(person, checkerMap.getOrDefault(person, 0) + 1); + } + + for (String person : completion) { + checkerMap.put(person, checkerMap.getOrDefault(person, 0) - 1); + } + + Iterator> it = checkerMap.entrySet().iterator(); + + while (it.hasNext()) { + + Entry mapEntry = it.next(); + + if (mapEntry.getValue() == 1) { + return mapEntry.getKey(); + } + } + return null; + } } \ No newline at end of file