슈뢰딩거의 고등어

[2022 Kakao blind recruitment] 신고 결과 받기 본문

알고리즘

[2022 Kakao blind recruitment] 신고 결과 받기

슈뢰딩거의 고등어 2022. 3. 23. 11:15

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

 

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

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

programmers.co.kr

 

[풀이방법]

1. 아이디에 따른 번호를 부여한다.

map <string, int> m 형식으로 저장한다. int 는 id_list 의 순서대로

2. 신고자와 신고된 유저의 관계를 arr 에 저장해준다.

신고자 a 신고된 유저 b 의 경우 arr[a][b] = true 로 나타내 준다.

bool 형식으로 저장한 이유는 각 유저들은 같은 유저를 여러번 신고할 수 있지만, 동일한 유저에 대한 신고횟수는 1회로 처리가 되기때문이다.

3. 각 유저마다 얼마나 신고가 되었는지 확인한다.

arr[0][b], arr[1][b], ... arr[마지막번호][b] 까지 true 인 갯수를 센다.

4. 신고된 횟수가 k 가 넘는지 확인한다.

5. k 번 이상 신고가 되었다면, 유저b를 신고한 유저들에게 메세지를 보낸다.

arr[a][b] 까지 true 인 a 유저들에 대한 Message 를  ++ 해준다.

 

[코드]

#include <string>
#include <vector>
#include <sstream>
#include <map>

using namespace std;

map <string, int> m;
bool arr[1000][1000];
vector <int> message;

void send_message(int target_no) {
    for(int i=0; i<m.size(); i++) {
        if(arr[i][target_no] == true) {
            message[i]++;
        }
    }
}

vector<int> solution(vector<string> id_list, vector<string> report, int k) {

    message.resize(id_list.size(), 0);
    
    for(int i=0; i<id_list.size(); i++) {
        string id = id_list[i];
        m.insert(make_pair(id, i));
    }
    
    for(auto r : report) {
        string s[2];
        istringstream stt(r);
        stt >> s[0] >> s[1];
        int a = m[s[0]];
        int b = m[s[1]];
        
        arr[a][b] = true;
    }
    
    
    for(int col=0; col<id_list.size(); col++) {
        int cnt = 0;
        for(int row=0; row<id_list.size(); row++) {
            if(arr[row][col] == true)
                cnt++;
        }
        if(cnt >= k) {
            send_message(col);
        }
    }
    return message;
}
Comments