Skip to content

프로그래머스 완주하지 못한 선수#99

Merged
honux77 merged 1 commit intocode-squad:be2020from
honux77:honux77
Feb 7, 2020
Merged

프로그래머스 완주하지 못한 선수#99
honux77 merged 1 commit intocode-squad:be2020from
honux77:honux77

Conversation

@honux77
Copy link
Contributor

@honux77 honux77 commented Feb 7, 2020

스트림으로 풀었다가 느려서 그냥 이터레이션으로 바꿈
분류: 해시
난이도: 2점

스트림으로 풀었다가 느려서 그냥 이터레이션으로 바꿈
분류: 해시
난이도: 2점
@honux77 honux77 merged commit aa278ec into code-squad:be2020 Feb 7, 2020
@honux77 honux77 deleted the honux77 branch February 7, 2020 00:12
@honux77
Copy link
Contributor Author

honux77 commented Feb 7, 2020

다른 사람의 풀이보니 너무 깔끔 ㅜㅜ
해시의 getOrDefault(k, v)를 사용하면 널 체크를 안 해도 되는구나.
난 널 ...

class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        HashMap<String, Integer> hm = new HashMap<>();
        for (String player : participant) hm.put(player, hm.getOrDefault(player, 0) + 1);
        for (String player : completion) hm.put(player, hm.get(player) - 1);

        for (String key : hm.keySet()) {
            if (hm.get(key) != 0){
                answer = key;
            }
        }
        return answer;
    }
}

@ksundong
Copy link
Contributor

ksundong commented Feb 9, 2020

앗.. 혹시 알고리즘 풀 때, foreach를 사용해도 괜찮은걸까요?
정말 짧은 지식으로 알고있는 건 for문보다 foreach가 더 느리다고 알고있어서 문제 풀 때,
일부러 for문을 사용했는데 그럴 필요가 없나해서요!
https://siyoon210.tistory.com/99
찾아보니 크게 신경쓸 정도로 차이가 나진 않네요... 다음풀이 부터는 foreach를 적극 활용해봐야겠습니다.

@honux77
Copy link
Contributor Author

honux77 commented Feb 9, 2020

@ksundong 대회냐 코딩 테스트냐에 따라 다른데요. 코딩 테스트는 그렇게 심각한 경우가 별로 없습니다. 대회에서는 잘 사용하지 않아요. 직접 재 봤는데 스트림은 좀 더 트리고, for iteration(?)은 그렇게 느리지 않았던 것 같습니다.

@ksundong
Copy link
Contributor

ksundong commented Feb 9, 2020

감사합니다! 참고하겠습니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants