Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.02.14 - [PG] 완주하지 못한 선수 #15

Closed
suhyunsim opened this issue Feb 14, 2021 · 0 comments
Closed

21.02.14 - [PG] 완주하지 못한 선수 #15

suhyunsim opened this issue Feb 14, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 해시 해시

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 14, 2021

문제

핵심 아이디어

  • 두 참가자, 완료자 배열을 정렬하고 인덱스 다른 선수 리턴

어려운 점, 실수

풀이

배열로 풀이

package main.java.com.poogle.PG.Q42576;

import java.util.Arrays;

public class Solution {
    public String solution(String[] participant, String[] completion) {
        Arrays.sort(participant);
        Arrays.sort(completion);
        int playerIndex = 0;

        for (playerIndex = 0; playerIndex < completion.length; playerIndex++) {

            if (!participant[playerIndex].equals(completion[playerIndex])) {
                return participant[playerIndex];
            }
        }
        return participant[playerIndex];
    }
}

Map Key로 풀이

import java.util.Map;
import java.util.HashMap;

class Solution {
    
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        Map<String, Integer> map = new HashMap<>();
        for (String person : participant) {
            map.put(person, map.getOrDefault(person, 0) + 1);
        }
        
        for (String player : completion) {
            map.put(player, map.get(player) - 1);
        }
        
        for (String key : map.keySet()) {
            if (map.get(key) != 0) {
                answer = key;
                break;
            }
        }
        
        return answer;        
    }
}

EntrySet으로 풀이

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

class Solution {
    
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        Map<String, Integer> map = new HashMap<>();
        for (String person : participant) {
            map.put(person, map.getOrDefault(person, 0) + 1);
        }
        
        for (String player : completion) {
            map.put(player, map.get(player) - 1);
        }
        
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            if (entry.getValue() != 0) {
                answer = entry.getKey();
                break;
            }
        }
        return answer;        
    }
}
@suhyunsim suhyunsim self-assigned this Feb 14, 2021
@suhyunsim suhyunsim added the lv.1 프로그래머스 - level 1 label Feb 14, 2021
@suhyunsim suhyunsim added 성공 맞은 문제 해시 해시 labels Feb 14, 2021
@suhyunsim suhyunsim added this to the 2월 2주 차 milestone Feb 14, 2021
@suhyunsim suhyunsim changed the title 21.02.14 - [Programmers] 완주하지 못한 선수 21.02.14 - [PG] 완주하지 못한 선수 Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 성공 맞은 문제 해시 해시
Projects
None yet
Development

No branches or pull requests

1 participant