Skip to content

Conversation

@Yoepee
Copy link
Member

@Yoepee Yoepee commented Jul 7, 2025

📌 문제 링크

(문제 링크 또는 문제 캡처본을 첨부해주세요.)
프로그래머스 42576번 - 완주하지 못한 선수
image


📝 문제 개요

  • 마라톤 경기에 참가한 선수들의 이름이 담긴 배열 participant가 주어집니다.
  • 그 중 한 명을 제외한 모든 선수가 마라톤을 완주했으며, 완주한 선수들의 명단은 completion 배열에 담겨 있습니다.
  • completion 배열에는 participant보다 1명이 적은 길이를 가집니다.
  • 완주하지 못한 선수의 이름을 반환하세요.

🧩 풀이 과정 요약

풀이 1: 정렬 후 비교

1.두 배열을 사전 순 정렬
2.앞에서부터 인덱스별로 비교
3.처음 불일치하는 사람 = 완주 못한 사람
4.모두 같으면 마지막 참가자가 미완주자

풀이 2: HashMap 활용 (이름별 인원 수)

  1. 참가자 명단을 Map<String, Integer>에 저장 (동명이인 고려)
  2. 완주한 사람은 count를 -1
  3. 최종적으로 value가 1인 key가 완주하지 못한 사람

😁 결과

(문제 해결 결과를 첨부해주세요.)
image

Copy link
Collaborator

@jjuchan jjuchan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

public static String solution2(String[] participant, String[] completion) {
String answer = "";
Map<String, Integer> map = new HashMap<>();
for(int i =0; i<participant.length;i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 예전에 getOrDefault를 써서 풀었는데,
merge 방식은 처음 봐서 배워갑니다!

Copy link
Collaborator

@jjuchan jjuchan Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이게 제 코드 였습니다.!!

// getOrDefault를 사용해 기존 값이 없으면 0으로 초기화해 카운트를 누적합니다.
for (String p : participant) {
map.put(p, map.getOrDefault(p, 0) + 1);
}

// 완주자 목록을 돌며 참가 횟수를 1씩 차감합니다. 중복처리
for (String c : completion) {
map.put(c, map.get(c) - 1);
}

// map을 순회하며 값이 0이 아닌 key가 완주하지 못한 선수임을 찾습니다.
for (String key : map.keySet()) {
if (map.get(key) != 0) {
return key;
}
}

@Yoepee Yoepee self-assigned this Jul 8, 2025
@jjuchan jjuchan self-requested a review July 14, 2025 07:03
@Yoepee Yoepee merged commit ea34051 into main Jul 14, 2025
1 check passed
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.

3 participants