๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.1 : ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ (์นด์นด์˜ค ๊ธฐ์ถœ)

728x90

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

 

2022 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฝ”ํ…Œ ๊ธฐ์ถœ

 

๋ฌธ์ œ ์„ค๋ช…

์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์‹ ๊ณ  ํšŸ์ˆ˜์— ์ œํ•œ์€ ์—†์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์œ ์ €๋ฅผ ๊ณ„์†ํ•ด์„œ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ์œ ์ €์— ๋Œ€ํ•œ ์‹ ๊ณ  ํšŸ์ˆ˜๋Š” 1ํšŒ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋œ ์œ ์ €๋Š” ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋˜๋ฉฐ, ํ•ด๋‹น ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ์œ ์ €์—๊ฒŒ ์ •์ง€ ์‚ฌ์‹ค์„ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.
    • ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ทจํ•ฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ํ•œ๊บผ๋ฒˆ์— ๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€๋ฅผ ์‹œํ‚ค๋ฉด์„œ ์ •์ง€ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ „์ฒด ์œ ์ € ๋ชฉ๋ก์ด ["muzi", "frodo", "apeach", "neo"]์ด๊ณ , k = 2(์ฆ‰, 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•˜๋ฉด ์ด์šฉ ์ •์ง€)์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

 

์œ ์ € ID์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID์„ค๋ช…

"muzi" "frodo" "muzi"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"apeach" "frodo" "apeach"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"frodo" "neo" "frodo"๊ฐ€ "neo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"muzi" "neo" "muzi"๊ฐ€ "neo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"apeach" "muzi" "apeach"๊ฐ€ "muzi"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์œ ์ € ID์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜

"muzi" 1
"frodo" 2
"apeach" 0
"neo" 2

์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ "frodo"์™€ "neo"์˜ ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ํ•œ ์•„์ด๋””์™€ ์ •์ง€๋œ ์•„์ด๋””๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์œ ์ € ID์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID์ •์ง€๋œ ID

"muzi" ["frodo", "neo"] ["frodo", "neo"]
"frodo" ["neo"] ["neo"]
"apeach" ["muzi", "frodo"] ["frodo"]
"neo" ์—†์Œ ์—†์Œ

๋”ฐ๋ผ์„œ "muzi"๋Š” ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 2ํšŒ, "frodo"์™€ "apeach"๋Š” ๊ฐ๊ฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 1ํšŒ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด์šฉ์ž์˜ ID๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด id_list, ๊ฐ ์ด์šฉ์ž๊ฐ€ ์‹ ๊ณ ํ•œ ์ด์šฉ์ž์˜ ID ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด report, ์ •์ง€ ๊ธฐ์ค€์ด ๋˜๋Š” ์‹ ๊ณ  ํšŸ์ˆ˜ k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ ์œ ์ €๋ณ„๋กœ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 


 

๊ตฌํ˜„์€ ์‰ฌ์šด๋ฐ ์‹œ๊ฐ„์ดˆ๊ณผ ๋•Œ๋ฌธ์— ๋งŽ์ด ํ—ค๋ฉจ๋‹ค!!

 

์ฒ˜์Œ์—๋Š” map์— <์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ, ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ vector> ๋กœ ํ•ด์„œ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ ๊ฐ’ ์ค‘๋ณต์ œ๊ฑฐ๋Š” ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค์„œ ํ–ˆ๊ณ 

๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋ฅผ vector์— ๊ธฐ๋กํ•ด์„œ ์ฐพ์•˜๋Š”๋ฐ

vector์—์„œ k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ ์ธ๋ฑ์Šค ์ฐพ๊ณ -> map์˜ vector์—์„œ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ ์žˆ๋Š”์ง€ ์ฐพ๊ณ 

-> ์žˆ์œผ๋ฉด ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ์—๊ฒŒ ๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ ๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋จ

๊ทผ๋ฐ ์ด๊ฑด 3๋ฒˆ์ด๋ž‘ 21๋ฒˆ์ด ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๋‚จ;

 

๊ทธ๋ž˜์„œ ๋‹ค์‹œ..๋˜๋Œ์•„๊ฐ€์„œ

๋ฐ˜๋Œ€๋กœ map์— <์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ, ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ set> ์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค

 

์ผ๋‹จ set์ด๋‹ˆ๊นŒ ์ค‘๋ณต์ œ๊ฑฐ๋Š” ์ž๋™์œผ๋กœ ๋˜๊ณ ,

์ด๋ ‡๊ฒŒ ์ €์žฅํ•ด๋†“์œผ๋‹ˆ๊นŒ ์‹ ๊ณ ๋‹นํ•œ ์‚ฌ๋žŒ.size()๊ฐ€ k ์ด์ƒ์ผ ๋•Œ ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ธ๋‹ค<< ๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋๋Š”๋ฐ

๊ธฐ์กด ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹ค ๋” ๊ฐ„๋‹จํ•ด์กŒ๊ณ , ์‹œ๊ฐ„๋„ ๋งŽ์ด ์ค„์—ˆ๋‹ค!

 

์ค‘๋ณต์ œ๊ฑฐ๋Š” set, ๊ทธ๋ฆฌ๊ณ  map ์ ‘๊ทผ๋ฒ•์€ auto ๋กœ ํ•˜๊ธฐ...โœ๐Ÿป

์นด์นด์˜ค ๊ธฐ์ถœ์„ ๋ณด๋‹ˆ ์ด ๊ธฐ์—…์€ ์•ฝ๊ฐ„ ์ž๋ฃŒ๊ตฌ์กฐ ์ง€์‹์ด ๋งŽ์•„์•ผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ์ข‹์•„ํ•˜๋Š”๋“ฏ. 

 

 

๋‚˜์˜ ํ’€์ด

#include <string>
#include <vector>
#include <map>
#include <set>
#include <iostream>
using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer(id_list.size(), 0);
    map<string,set<string> > does; //์‹ ๊ณ ๋‹นํ•œ ๋‹‰, ์‹ ๊ณ ํ•œ ๋‹‰ ๋ชฉ๋ก
    map<string, int> name_idx; // ๋‹‰๋„ค์ž„&์ธ๋ฑ์Šค
    string n1 = "";
    string n2 = "";
    for(int i=0; i<id_list.size(); ++i) name_idx[id_list[i]] = i;
    for(int i=0; i<report.size();++i){
        for(int j=0; j<report[i].length();++j){
            if(report[i][j]==' '){
                n1 = report[i].substr(0,j);
                n2 = report[i].substr(j+1);
                break;
            }
        }
        
        does[n2].insert(n1);
    }
    
    for(auto i : does){
        if(i.second.size() >= k){ // k์ด์ƒ ์‹ ๊ณ ๋จน์œผ๋ฉด ์ •์ง€
            for(auto k : i.second){
                int idx = name_idx[k];
                answer[idx]++;
            }
        }
    }
    return answer;
}

 

728x90