[프로그래머스][Level1] 신고 결과 받기 c++

2022. 9. 3. 13:59·🍞 Algorithm/Programmers
https://programmers.co.kr/learn/courses/30/lessons/92334 
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

풀이과정

풀이 (시간초과 1개)

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

using namespace std;

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

    map<pair<string, string>, int> repo; // report
    map<string, int> s; // 신고받은 유저

    for(int i = 0; i < report.size(); i++){
        int idx = report[i].find(' ');
        string from = report[i].substr(0, idx);
        string to = report[i].substr(idx);

        repo[{from, to}]++;
        if(repo[{from, to}] == 1) s[to]++;
    }
    vector<string> v; // 정지받은 유저
    for(auto iter : s){
        if(iter.second >= k) v.push_back(iter.first);
    }

    map<string, int> ans;
    for(int i = 0; i < v.size(); i++){
        for(auto iter : repo){
            if(iter.first.second == v[i]){
                ans[iter.first.first]++;
            }
        }
    }
    for(int i = 0; i < id_list.size(); i++){
        int cnt = 0;
        for(auto iter : ans){
            if(id_list[i] == iter.first){
                answer.push_back(iter.second);
                cnt++;
            }
        }
        if(!cnt) answer.push_back(0);
    }
    return answer;
}

 

풀이 (시간초과 2개)

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

using namespace std;

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

    vector<vector<string>> repo;
    for(int i = 0; i < report.size(); i++){
        string str = "";
        vector<string> temp;
        for(int j = 0; j < report[i].size(); j++){
            if(report[i][j] == ' '){
                temp.push_back(str);
                str = "";
            }
            else str += report[i][j];
        }
        if(str != "") temp.push_back(str);
        repo.push_back(temp);
    }

    map<pair<string, string>, int> m; // m -> f, 1
    map<string, int> s; // 신고 받은 횟수
    for(int i = 0; i < repo.size(); i++){
        m[{repo[i][0], repo[i][1]}]++;
        if(m[{repo[i][0], repo[i][1]}] == 1){
            s[repo[i][1]]++;
        }
    }

    vector<string> v; // 정지된 유저
    for(auto i : s){
        if(i.second >= k)  v.push_back(i.first);
    }

    for(int i = 0; i < id_list.size(); i++){
        int cnt = 0;
        for(auto iter : m){
            if(id_list[i] == iter.first.first){
                for(int j = 0; j < v.size(); j++){
                    if(v[j] == iter.first.second){
                        cnt++;
                    }
                }
            }
        }
        answer.push_back(cnt);
    }
    return answer;
}
저작자표시 (새창열림)

'🍞 Algorithm > Programmers' 카테고리의 다른 글

[프로그래머스][Level1] 크레인 인형뽑기 게임 c++  (0) 2022.09.03
[프로그래머스][Level1] 신규 아이디 추천 c++  (0) 2022.09.03
[프로그래머스][Level1] 문자열 내 마음대로 정렬하기 c++  (0) 2022.09.03
[프로그래머스][Level1] 2016년 c++  (0) 2022.09.03
[프로그래머스][Level1] 음양 더하기 c++  (0) 2022.09.03
'🍞 Algorithm/Programmers' 카테고리의 다른 글
  • [프로그래머스][Level1] 크레인 인형뽑기 게임 c++
  • [프로그래머스][Level1] 신규 아이디 추천 c++
  • [프로그래머스][Level1] 문자열 내 마음대로 정렬하기 c++
  • [프로그래머스][Level1] 2016년 c++
박빵이
박빵이
2025년에도 갓생살기
  • 박빵이
    기억보다 기록
    박빵이
  • 전체
    오늘
    어제
    • 분류 전체보기 (337)
      • 🍞 FrontEnd (97)
        • HTML+CSS (4)
        • JavaScript (17)
        • TypeScript (4)
        • React (52)
        • Next.js (2)
        • Android (15)
      • 🍞 BackEnd (24)
        • Java (15)
        • Node.js (6)
        • Spring (1)
      • 🍞 Cloud & Infra (0)
        • AWS SAA (0)
        • Microsoft Azure (0)
      • 🍞 Algorithm (147)
        • C++ (4)
        • Baekjoon (41)
        • Programmers (97)
      • 🍞 Computer Science (18)
        • 운영체제 (1)
        • 데이터 통신 (6)
        • 네트워크 (6)
        • 데이터베이스 (1)
      • 🍞 대외활동 & 부트캠프 (42)
        • 삼성 청년 SW 아카데미 (1)
        • LG유플러스 유레카 (0)
        • 한국대학생IT경영학회 (1)
        • IT연합동아리 UMC (17)
        • 길벗 블로깅 멘토 (18)
        • IT연합동아리 피로그래밍 (3)
        • 개발 컨퍼런스 (2)
  • 블로그 메뉴

    • Admin
  • 링크

    • GitHub
  • 인기 글

  • 태그

    JavaScript
    C++
    알고리즘
    react
    umc
    Front
    길벗 블로깅 멘토링
    유니온파인드
    길벗 블로깅 멘토
    Android
    level2
    level1
    코딩자율학습
    위상정렬
    map
    프로그래머스
    백준
    안드로이드
    Java
    코틀린
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
박빵이
[프로그래머스][Level1] 신고 결과 받기 c++
상단으로

티스토리툴바