-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 2주차 - PG_42576 완주하지 못한 선수 [김동엽] #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # 📌 문제 링크 | ||
| (문제 링크 또는 문제 캡처본을 첨부해주세요.) | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| ## 📝 문제 개요 | ||
| - | ||
|
|
||
| --- | ||
|
|
||
| ## 🧩 풀이 과정 요약 | ||
| - | ||
|
|
||
| --- | ||
|
|
||
| ## 😁 결과 | ||
| (문제 해결 결과를 첨부해주세요.) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package week01.Yoepee; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class PG_42576 { | ||
| public static String solution(String[] participant, String[] completion) { | ||
| String answer = ""; | ||
| Arrays.sort(participant); | ||
| Arrays.sort(completion); | ||
| for(int i =0; i<participant.length; i++){ | ||
| if (i == participant.length-1) return participant[i]; | ||
| if (participant[i].equals(completion[i]) == false) { | ||
| return participant[i]; | ||
| } | ||
| } | ||
| return answer; | ||
| } | ||
|
|
||
| public static String solution2(String[] participant, String[] completion) { | ||
| String answer = ""; | ||
| Map<String, Integer> map = new HashMap<>(); | ||
| for(int i =0; i<participant.length;i++){ | ||
| map.merge(participant[i], 1, (v1, v2) -> Math.max(v1,v2)+1); | ||
| } | ||
| for(int i =0; i<completion.length;i++){ | ||
| map.merge(completion[i], 0, (v1, v2) -> v1-1); | ||
| } | ||
| for(int i =0; i<participant.length;i++){ | ||
| if(map.get(participant[i]) == 1){ | ||
| answer =participant[i]; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| // 정상 결과: "leo" | ||
| System.out.println(solution(new String[]{"leo", "kiki", "eden"}, new String[]{"eden", "kiki"})); | ||
| // 정상 결과: "vinko" | ||
| System.out.println(solution(new String[]{"marina", "josipa", "nikola", "vinko", "filipa"}, new String[]{"josipa", "filipa", "marina", "nikola"})); | ||
| // 정상 결과: "mislav" | ||
| System.out.println(solution(new String[]{"mislav", "stanko", "mislav", "ana"}, new String[]{"stanko", "ana", "mislav"})); | ||
| // 정상 결과: "b" | ||
| System.out.println(solution(new String[]{"a", "b", "a", "c", "d"}, new String[]{"a", "a", "c", "d"})); | ||
| // 정상 결과: "b" | ||
| System.out.println(solution(new String[]{"a", "a", "b", "a", "c", "d"}, new String[]{"a", "a", "a", "c", "d"})); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 예전에 getOrDefault를 써서 풀었는데,
merge 방식은 처음 봐서 배워갑니다!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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;
}
}