코딩 테스트 연습 시작
#include <bits/stdc++.h>
using namespace std;
// string split
vector<string> split(string str){
istringstream iss(str);
string buff;
vector<string> result;
while(getline(iss, buff, ' ')){
result.push_back(buff);
}
return result;
}
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
unordered_map<string, int> id_map;
for(int i=0; i<id_list.size(); i++)
id_map.insert(pair<string, int>(id_list[i], 0));
map<string, set<string>> cnt; // 신고한 유저들의 set
for(string ids: report){
vector<string> id = split(ids); // split
cnt[id[1]].insert(id[0]);
}
for(int i=0; i<id_list.size(); i++){
if(cnt[id_list[i]].size() >= k){
for(string id: cnt[id_list[i]]){
id_map[id]++;
}
}
}
vector<int> answer;
for(int i=0; i<id_list.size(); i++){
answer.push_back(id_map[id_list[i]]);
}
return answer;
}
문제 정리:
https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
input
- vector<string> id_list : 유저 리스트
- vector<string> report : "a b" 형태, a 유저가 b를 신고했다
- int k : k번 이상 신고가 들어오면 정지
- 한 유저가 어느 유저를 한번 이상 신고하더라도 신고는 한번만 카운트된다.
- k번 이상 신고가 들어와서 정지 당한 유저를 신고했던 유저에게 메일을 보낸다
output
- 유저가 메일을 받은 횟수를 id_list에 적힌 순서대로 vector<int>로 출력
[접근1]
벡터 report에 들어있는 string "a b"를 "a"와 "b"로 분리해서
신고당한 유저와 그 유저를 신고한 유저들의 set을 저장한 map을 만들면
.size()를 이용해서 몇명이 신고했는지 바로 알 수 있고,
k명이 넘으면 메일을 보내야하는 유저들도 바로 알 수 있을듯
(python과 헷갈려서 c++에 split이 있는줄 착각했다.. 안되길래 구글링 "c++ split" 검색..)
크게 3가지 정도가 있었고, 코딩을 할땐 split 함수가 없다는것에 당황해서 눈에 보이는걸로 짰었다.
내가 잘 쓸거같은 두가지만 정리
vector<string> split(string str, char delimiter){ // delimiter: 구분자
istringstream iss(str);
string buff;
vector<string> result;
while(getline(iss, buff, delimeter)){
result.push_back(buff);
}
return result;
}
// 이 함수를 아래와같이 사용하였다. //
map<string, set<string>> cnt; // 신고한 유저들의 set
for(string ids: report){
vector<string> id = split(ids); //split
cnt[id[1]].insert(id[0]);
}
하지만 이것은 delimiter가 공백이나 \n가 아닌 문자일 때 사용해야 의미가 있을거같다.
여기 문제에서는 공백으로 분리하면 되기때문에 다음엔 아래와같은 방법을 사용할거같다. (실제로 돌려보진 않음)
map<string, set<string>> cnt; // 신고한 유저들의 set
for(string ids : report){
istringstream iss(ids);
string a, b;
iss >> a >> b; //split
cnt[b].insert(a);
}
[접근2]
만들어놓은 map인 cnt를 돌면서 해당 유저가 신고받은 횟수가 k번이 넘는지 확인
k번이 넘으면, 해당 유저의 value인 set을 돌면서 해당 유저를 신고했던 유저들에게 메일을 보냄(횟수 증가)
해당 유저가 메일 받은 횟수를 증가시킬 때 간단하게 접근하기 위해서 map으로 만든 변수 id_map을 사용
unordered_map<string, int> id_map;
for(int i=0; i<id_list.size(); i++)
id_map.insert(pair<string, int>(id_list[i], 0));
for(int i=0; i<id_list.size(); i++){
if(cnt[id_list[i]].size() >= k){
for(string id: cnt[id_list[i]]){
id_map[id]++;
}
}
}
(3개나 중첩이 돼서 마음이 많이 불편하다..)
[접근3]
id_list를 순서대로 돌면서 해당 유저가 메일을 받은 횟수를 id_map을 통해 알아내서 answer에 push_back
vector<int> answer;
for(int i=0; i<id_list.size(); i++){
answer.push_back(id_map[id_list[i]]);
}'coding > algorithm' 카테고리의 다른 글
| [programmers] Lv.2 오픈채팅방(c++) (0) | 2022.04.18 |
|---|---|
| [programmers] Lv.2 문자열 압축(c++) (0) | 2022.04.15 |
| [programmers] Lv.1 숫자 문자열과 영단어(c++) (0) | 2022.03.08 |
| [programmers] Lv.1 신규 아이디 추천(c++) (0) | 2022.03.01 |
| [programmers] Lv.1 로또의 최고 순위와 최저 순위(c++) (0) | 2022.02.28 |
댓글