https://programmers.co.kr/learn/courses/30/lessons/42576
정확성 정답 -> 효율성에서 틀림
#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());
int saveHash=0;
for(int i=0;i<participant.size();i++){
for(int j=0;j<completion.size();j++){
if(participant[i].compare(completion[j])==0){
participant[i]=" ";
completion[j]=" ";
continue;
}
}
}
for(int i=0;i<participant.size();i++){
if( participant[i]!=" "){
answer=participant[i];
}
}
return answer;
}
2020/04/01 - [C++(Math&알고리즘)] - Hash STL
이용을 해서 효율성 문제를 해결
#include <string>
#include <vector>
#include<unordered_map>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_map<string ,int > key;
for(auto&i:participant)
key[i]++;
for(auto&i:completion)
key[i]--;
for(auto&i:key){
if(i.second>0){
answer=i.first;
break;
}
}
return answer;
}
200816
지난번과 비슷하게 푼것같다.
#include <string>
#include <vector>
#include<unordered_map>
#include<iostream>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_map<string,int>part;
for(int i=0;i<participant.size();i++){
part[participant[i]]++;
}
for(int i=0;i<completion.size();i++){
part[completion[i]]--;
}
for(int i=0;i<participant.size();i++){
if( part[participant[i]]==1){
answer=participant[i];
}
}
return answer;
}
unordered_map은 일반적으로
문자와 숫자가 같이 혼용될때 사용하기 좋은것같다.
특히나 내가 원하는 string 자료들 비교 할떄 굉장히 유용한것같다.
201007
해시가 키가 같으면 동시에 값이 들어간다는걸 잊지말자
#include <string>
#include <vector>
#include <string.h>
#include<unordered_map>
#include<cstring>
#include<iostream>
using namespace std;
string solution(vector<string> participant, vector<string> completion) {
string answer = "";
unordered_map<string,int>ready;
unordered_map<string,int>finish;
for(int i=0;i<participant.size();i++){
ready[participant[i]] +=1;
}
for(int i=0;i<completion.size();i++){
ready[completion[i]] -=1;
}
for(int i=0;i<participant.size();i++){
if(ready[participant[i]]==1){
answer=participant[i];
i=participant.size();
}
}
// answer="";
return answer;
}
'알고리즘 공부' 카테고리의 다른 글
프로그래머스 level 3 연습-여행경로 (0) | 2020.04.04 |
---|---|
프로그래머스 level -2 연습-다리를 지나는 트럭 (0) | 2020.04.03 |
프로그래머스 공부 level2 -문자열 압축 (0) | 2020.03.07 |
삼각 함수 & 구면좌표계-01 (0) | 2020.02.26 |
프로그래머스 level -3 코딩 연습-단어 변환 (0) | 2020.02.21 |