무민은귀여워

[프로그래머스] 2022 KAKAO BLIND RECRUITMENT 신고 결과 받기 본문

IT/알고리즘

[프로그래머스] 2022 KAKAO BLIND RECRUITMENT 신고 결과 받기

moomini 2022. 5. 16. 18:00
반응형

1. 문제

2. 풀이

2-1. 첫번째 풀이

2-2. 두번째 풀이

1. 문제

https://programmers.co.kr/learn/courses/30/lessons/92334

 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

2. 풀이

첫번째 풀이

1. 신고 리스트에서 한 유저가 동일한 유저에 대한 신고를 제외시킨다

2. 신고 리스트를 읽어, 유저별 누적 신고수를 저장

3. 신고 리스트를 읽어 정지 대상자를 확인하여, 유저별 받을 메일 수 저장  

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer(id_list.size(), 0); // 유저가 받을 결과 메일 수
map<string, int> id_map; // id 인덱스 참조용
for (int i = 0; i < id_list.size(); i++)
{
id_map.insert(make_pair(id_list[i], i));
}
vector<int> report_cnt(id_list.size(), 0); // 누적 신고수
// 동일한 유저에 대한 신고 제거
sort(report.begin(), report.end());
report.erase(unique(report.begin(), report.end()), report.end());
// 누적 신고수 기록
for (auto str : report)
{
string name2;
name2 = str.substr(str.find(" ") + 1, str.length());
for (int n = 0; n < id_list.size(); n++)
{
if (strcmp(name2.c_str(), id_list[n].c_str()) == 0)
{
report_cnt[n] += 1;
}
}
}
// 유저가 받을 결과 메일 수 담기
for (auto str : report)
{
string name1, name2;
name1 = str.substr(0, str.find(" "));
name2 = str.substr(str.find(" ") + 1, str.length());
int idx1 = id_map[name1];
int idx2 = id_map[name2];
// name2가 정지대상자인지 확인
if (report_cnt[idx2] >= k)
{
answer[idx1] += 1;
}
}
return answer;
}

두번째 풀이

처음 풀이에서 신고리스트를 두번 읽고 있으므로... 다른 풀이를 보고 수정

1. 신고 리스트에서 한 유저가 동일한 유저에 대한 신고를 제외시킨다

2. 신고 리스트를 읽어, 유저별 신고 리스트를 만든다

3. 유저별 신고 리스트를 읽어 정지 대상자를 확인하여, 유저별 받을 메일 수 저장 

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer(id_list.size(), 0); // 유저가 받을 결과 메일 수
map<string, int> id_map; // id 인덱스 참조용
for (int i = 0; i < id_list.size(); i++)
{
id_map.insert(make_pair(id_list[i], i));
}
// 동일한 유저에 대한 신고 제거
sort(report.begin(), report.end());
report.erase(unique(report.begin(), report.end()), report.end());
// 유저별 신고 기록
vector<pair<int, int>> report_list;
for (auto str : report)
{
string name1, name2;
name1 = str.substr(0, str.find(" "));
name2 = str.substr(str.find(" ") + 1, str.length());
report_list.push_back({ id_map[name1], id_map[name2] });
}
// 유저가 받을 결과 메일 수 담기
vector<int> report_cnt(id_list.size(), 0);
for (auto elem : report_list) report_cnt[elem.second]++;
for (auto elem : report_list) if (report_cnt[elem.second] >= k) answer[elem.first]++;
return answer;
}

 

반응형
Comments