250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 인덕원존맛
- 코테사이트
- 인덕원역맛집
- 알고리즘
- 프로그래머스
- 코딩테스트사이트
- 릿코드
- 개발자취업
- 개발자알고리즘
- 인덕원고기집
- 제주놀거리
- 아마존릿코드
- 프로그래머스코딩테스트
- 구글릿코드
- 애플릿코드
- 인덕원맛집
- 인덕원카페
- 삼성맛집
- 강남역맛집
- 코딩테스트사이트추천
- 코딩테스트
- 인덕원고기
- 내돈내산
- 삼성역맛집
- 제주볼거리
- 제주맛집
- 평촌카페
- 안양고기
- 안양맛집
- 알고리즘해시
Archives
- Today
- Total
민여위-
[프로그래머스] 완주하지 못한 선수 (해시, 코딩테스트) 본문
728x90
반응형
1. 문제설명
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
제한사항
- 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다.
- completion의 길이는 participant의 길이보다 1 작습니다.
- 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다.
- 참가자 중에는 동명이인이 있을 수 있습니다.
입출력 예
participantcompletionreturn
["leo", "kiki", "eden"] | ["eden", "kiki"] | "leo" |
["marina", "josipa", "nikola", "vinko", "filipa"] | ["josipa", "filipa", "marina", "nikola"] | "vinko" |
["mislav", "stanko", "mislav", "ana"] | ["stanko", "ana", "mislav"] | "mislav" |
입출력 예 설명
예제 #1
"leo"는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #2
"vinko"는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #3
"mislav"는 참여자 명단에는 두 명이 있지만, 완주자 명단에는 한 명밖에 없기 때문에 한명은 완주하지 못했습니다.
2. 내 풀이
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool checkException(vector<string> _participant, vector<string> _completion) {
int participantSize = _participant.size();
int completionSize = _completion.size();
// exception 1
if (!(participantSize > 0 && participantSize <= 100000)) {
cout << "Exception 1" << endl;
return false;
}
// exception 2
if (1 != (participantSize - completionSize)) {
cout << "Exception 2" << endl;
return false;
}
// exception 3
for (int i = 0; i < participantSize; i++) {
if (_participant[i].length() < 1 && _participant[i].length() > 20) {
cout << "Exception 3" << endl;
return false;
}
}
for (int i = 0; i < completionSize; i++) {
if (_completion[i].length() < 1 && _completion[i].length() > 20) {
cout << "Exception 3" << endl;
return false;
}
}
return true;
}
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
int participantSize = participant.size();
int completionSize = completion.size();
if (checkException(participant, completion)) {
sort(participant.begin(), participant.end());
sort(completion.begin(), completion.end());
for (int i = 0; i < completionSize; i++) {
if (participant[i] != completion[i]) {
return participant[i];
}
}
return participant[participantSize-1];
} else {
cout << "catch exception in testcase" << endl;
}
return answer;
}
3. 다른 사람 풀이 (추천수 3위까지)
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
sort(participant.begin(), participant.end());
sort(completion.begin(), completion.end());
for(int i=0;i<completion.size();i++)
{
if(participant[i] != completion[i])
return participant[i];
}
return participant[participant.size() - 1];
//return answer;
}
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_map<string, int> strMap;
for(auto elem : completion)
{
if(strMap.end() == strMap.find(elem))
strMap.insert(make_pair(elem, 1));
else
strMap[elem]++;
}
for(auto elem : participant)
{
if(strMap.end() == strMap.find(elem))
{
answer = elem;
break;
}
else
{
strMap[elem]--;
if(strMap[elem] < 0)
{
answer = elem;
break;
}
}
}
return answer;
}
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_multiset<string> names;
for(int i = 0; i < participant.size(); i++)
{
names.insert(participant[i]);
}
for(int i = 0; i < completion.size(); i++)
{
unordered_multiset<string>::iterator itr = names.find(completion[i]);
names.erase(itr);
}
return *names.begin();
}
예외 처리는 따로 풀이에 안 나오는 듯.. 난 너무 븅신같이 푼다
해시문제라 map을 써야했을거같은데 깔깔
잘 푸는 날까지 아자아자.. ㅠㅠ
728x90
반응형
'Tech' 카테고리의 다른 글
[알고리즘] 코딩테스트 사전 준비 (c++ 기준) (0) | 2021.10.01 |
---|---|
[프로그래머스] 기능 개발 (스택/큐, 코딩테스트) (0) | 2021.10.01 |
[Javascript] Chrome App - Momemtum 만들기 (0) | 2021.08.21 |
[UE4] 언리얼엔진4 설치 및 빌드 (0) | 2021.08.20 |
[Javascript] 자바스크립트 기초 (0) | 2021.08.18 |
Comments