떵호
seongho'Dev
떵호
전체 방문자
오늘
어제
  • 분류 전체보기 (116)
    • 회고 (2)
    • Algorithm (74)
      • 프로그래머스 (65)
      • 백준(BOJ) (2)
      • Note (7)
    • 기술독서 (25)
      • Clean Code (11)
      • 자바의 정석 (8)
      • 대규모 시스템 설계 기초 (6)
    • Computer Science (1)
      • Operating System (1)
    • Typescript (1)
    • JAVA (0)
    • Spring (6)
      • JPA (6)
    • AWS (2)
    • Git (2)
    • Etc (2)

블로그 메뉴

  • github

티스토리

태그

  • 구현
  • 프로그래머스
  • 클린코드
  • 완전탐색
  • 자바의 정석
  • 코딩테스트 준비
  • 카카오 코테
  • Clean Code
  • 알고리즘
  • JPA
hELLO · Designed By 정상우.
떵호

seongho'Dev

[c++] 프로그래머스 - 신고 결과 받기 (2022 KAKAO BLIND RECRUITMENT)
Algorithm/프로그래머스

[c++] 프로그래머스 - 신고 결과 받기 (2022 KAKAO BLIND RECRUITMENT)

2022. 2. 9. 22:49
728x90

🔗 문제 링크

 

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

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

programmers.co.kr

📂 분류

문자열, 구현, 자료구조

💡 풀이

카카오 코테에서 자주 출제되는 문자열 처리 후 구현하는 문제이다.

우선 id를 매핑하기 위해 map이 필요하다.
그리고 유저 ID와 유저가 신고한 ID를 저장하는 자료구조를 map과 2차원 vector를 고민하다가 2차원 vector로 하기로 했다.

한 유저가 동일한 유저에 대한 신고 횟수는 1회이기 때문에 vector<vector<bool>>로 선언을 하고 report를 공백 분리한 후 유저 ID와 유저가 신고한 ID 테이블에 저장한다.

유저가 신고받은 횟수를 구하고 k번 이상 신고를 받은 유저 리스트를 따로 만들어 저장한다.

그리고 문제 설명대로 answer 값을 구하고 반환하면 된다.

💻 코드

#include <bits/stdc++.h>

using namespace std;

map<string, int> user;
vector<vector<bool>> reportList;
vector<int> numberOfReport;

void initList(vector<string> id_list) {
    int idx = 0;
    for (string s : id_list) {
        user[s] = idx;
        idx += 1;
    }
}

void splitAndEnterReportList(vector<string> report) {
    for (string s : report) {
        istringstream ss(s);
        string temp[2];
        ss >> temp[0] >> temp[1];

        int repoter, respondent;
        repoter = user[temp[0]]; // 신고를 한 사람
        respondent = user[temp[1]]; // 신고를 당한 사람
        reportList[repoter][respondent] = true;
    }
}

void countNumberOfReport(int len) {
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
            if (reportList[i][j])
                numberOfReport[j] += 1;
        }
    }
}


vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer, list;
    int len = (int)id_list.size();
    reportList.resize(len, vector<bool>(len, false));
    numberOfReport.resize(len, 0);
    list.reserve(len);
    answer.resize(len, 0);

    initList(id_list);
    splitAndEnterReportList(report);
    countNumberOfReport(len);

    for (int i = 0; i < len; i++) {
        if (numberOfReport[i] >= k) {
            list.push_back(i);
        }
    }

    for (int i = 0; i < len; i++) {
        for (int idx : list) {
            if (reportList[i][idx]) {
                answer[i] += 1;
            }
        }
    }

    return answer;
}
728x90
저작자표시 (새창열림)

'Algorithm > 프로그래머스' 카테고리의 다른 글

[자바] 프로그래머스 - 카펫 (Level 2)  (0) 2022.02.10
[c++] 프로그래머스 - 빛의 경로 사이클 (Level 2)  (0) 2022.02.10
[C++] 프로그래머스 - 더 맵게 (Level 2)  (0) 2022.02.09
[자바] 프로그래머스 - H-Index (Level 2)  (0) 2022.02.09
[자바] 프로그래머스 - 위장 (Level 2)  (0) 2022.02.08
    'Algorithm/프로그래머스' 카테고리의 다른 글
    • [자바] 프로그래머스 - 카펫 (Level 2)
    • [c++] 프로그래머스 - 빛의 경로 사이클 (Level 2)
    • [C++] 프로그래머스 - 더 맵게 (Level 2)
    • [자바] 프로그래머스 - H-Index (Level 2)
    떵호
    떵호

    티스토리툴바