완주하지 못한 선수(for JAVA)

Sunyoung·2021년 4월 21일
0

이 문제는 사실 자바스크립트로 조금 풀다 정확성 테스트는 다 통과하고 효율성에서 통과하지 못했었다 (indexOf를 썼기 때문인듯)
어쨌든 자바로 이번엔 해시를 무조건 쓰려고 했다

import java.util.*;

class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        HashMap<String, Integer> hm = new HashMap<String, Integer>();
        for(int i=0;i<completion.length;i++) {
            if(hm.get(completion[i]) == null) {
                hm.put(completion[i], 1); 
            } else {
                hm.put(completion[i], hm.get(completion[i])+1);   
            }
        }
        for(int a=0;a<participant.length;a++) {
            if (hm.get(participant[a]) != null) {
                if (hm.get(participant[a]) > 0) {
                    hm.put(participant[a], hm.get(participant[a])-1);   
                } else {
                    return participant[a];
                }
            } else {
                return participant[a];
            }
        }
        return answer;
    }
}
profile
배워서 남주자

0개의 댓글