알고리즘/프로그래머스
프로그래머스 신고결과받기 c++
이영재의오른발
2022. 6. 29. 11:20
반응형
https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
bool check[1001][1001]; // 신고했는지 안했는지
int lee[1001]; //몇 번 신고당했는지
int final[1001];
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer;
vector<pair<string,int>> v;
for(int i=0; i<id_list.size(); i++) {
v.push_back({id_list[i],i});
}
for(int i=0; i<report.size(); i++) {
vector<string> hello;
istringstream ss(report[i]);
string word;
while(getline(ss,word,' ')) {
hello.push_back(word);
}
string a = hello[0];
string b = hello[1];
int a1,b1;
for(int j=0; j<id_list.size(); j++) {
if(v[j].first == a) {
a1 = v[j].second;
}
if(v[j].first == b) {
b1 = v[j].second;
}
}
if(check[a1][b1]) {
continue;
}
else if(!check[a1][b1]){
check[a1][b1] = true;
lee[b1]++;
}
}
for(int i=0; i<id_list.size(); i++) {
if(lee[i] >= k) {
for(int j=0; j<id_list.size(); j++) {
if(check[j][i]) {
final[j]++;
}
}
}
}
for(int i=0; i<id_list.size(); i++) {
answer.push_back(final[i]);
}
return answer;
}
알고리즘 순서를 알아보자
1. <string,int> 로 된 vector를 만들고 그 vector 안에 id_list와 id_list의 인덱스 번호를 저장해준다.
2. report 를 공백 기준으로 분리해 첫번째 두번째 문자열의 인덱스 번호를 lee배열안에 넣고 lee배열을 증가시켜준다
3. bool check 를 통해 두번 신고하지않도록 해줌.
4. lee 배열이 k 보다 클 경우 신고 메일을 보내는 final 배열을 증가시켜줌.
코드 예쁘게 잘짰네!
728x90
반응형