Notice
Recent Posts
Recent Comments
Today
Total
01-26 23:33
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2022 KAKAO BLIND RECRUITMENT] ์‹ ๊ณ ๊ฒฐ๊ณผ๋ฐ›๊ธฐ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2022 KAKAO BLIND RECRUITMENT] ์‹ ๊ณ ๊ฒฐ๊ณผ๋ฐ›๊ธฐ

WonderJay 2022. 6. 26. 00:23
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

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

programmers.co.kr

 

string(id), set<string> (id ๋ฅผ ์‹ ๊ณ ํ•œ ์‚ฌ๋žŒ๋“ค)์„ key - value ๋กœ ๊ฐ€์ง€๋Š” unordered_map ์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•œ ์œ ์ €๊ฐ€ ๋‹ค๋ฅธ ์œ ์ €๋ฅผ 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ํ•œ ๊ฒƒ์€ 1๋ฒˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” set ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

report ๋ฐฐ์—ด์€ ๋„์›Œ์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ •๋ณด๊ฐ€ ๋“ค์–ด์˜จ๋‹ค. (์‹ ๊ณ ํ•œ์‚ฌ๋žŒ ์‹ ๊ณ ๋ฐ›์€์‚ฌ๋žŒ)

๊ทธ๋Ÿฌ๋ฏ€๋กœ  ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ํŒŒ์‹ฑ์ด ํ•„์š”ํ•œ๋ฐ, ์ด๋Š” stringstream ์œผ๋กœ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

report ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑํ•ด ์‹ ๊ณ ํ•œ์‚ฌ๋žŒ๊ณผ ์‹ ๊ณ ๋ฐ›์€ ์‚ฌ๋žŒ์˜ id ๋ฅผ ๋ถ„๋ฆฌํ•œ๋‹ค

report_map[second].insert(first) ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ, ์ด๋Š”

second ๋ผ๋Š” ์ด๋ฆ„์˜ id ์— ํ•ด๋‹นํ•˜๋Š” value ๊ฐ’์ธ set<string> ์— first ๋ผ๋Š” ์ด๋ฆ„์˜ id ๊ฐ€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  inserting ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ดํ›„, report_map ์„ ์ˆœํšŒํ•˜์—ฌ set ์˜ size ๊ฐ€ k ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ

set ์„ ์ˆœํšŒํ•˜์—ฌ, ํ•ด๋‹นํ•˜๋Š” id ์˜ index ๋ฅผ ++ ํ•ด์ฃผ๋ฉด๋œ๋‹ค.

์ด๋•Œ index ๋Š” ์ดˆ๊ธฐ์— index ๋ฅผ ์ €์žฅํ•˜๋Š” unordered_map<string, int> id_map;  ์„ ๋งŒ๋“ค๊ณ 

 

    for (int i = 0; i < id_list.size(); i++)
    {
        id_map.insert(make_pair(id_list[i], i));
    }

 

์™€ ๊ฐ™์ด ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์—ˆ๋‹ค.

 

#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
#include <iostream>
#include <set>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) {
    vector<int> answer(id_list.size(), 0); // answer ์ดˆ๊ธฐํ™”

    unordered_map<string, int> id_map;
    for (int i = 0; i < id_list.size(); i++)
    {
        id_map.insert(make_pair(id_list[i], i));
    }
    unordered_map<string, set<string>> report_map;
    for (auto ele : report)
    {
        stringstream ss(ele);
        string first, second;
        ss >> first, ss >> second;

        /*first ๊ฐ€ second ๋ฅผ ์‹ ๊ณ ํ•œ ๊ฒƒ*/
        report_map[second].insert(first);
        ss.clear();
    }

    for (auto ele : report_map)
    {
        if (ele.second.size() >= k)
        {
            for (auto iter : ele.second)
            {
                int index = id_map[iter];
                answer[index] ++;
            }
        }
    }

    return answer;
}

 

ํ’€์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ž˜ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ, ํ’€์ด๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์—ฌ๋Ÿฟ ์ฐธ๊ณ ํ•˜์˜€๋‹ค. ์ผ๋‹จ ๋งŽ์ด ํ’€๋ฉด์„œ ๊ฐ์„ ์ตํ˜€๋ณด์ž.

 

๋”๋ณด๊ธฐ

๋ฐฐ์šด ๊ฒƒ

1. ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— stringstream ์„ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

2. ์ƒ๊ฐ๋ณด๋‹ค ํ’€์ด ๋ฐฉ๋ฒ•์€ ๋‹จ์ˆœํ•˜๋‹ค. ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผํ•ด๋ณด์ž..

 

https://wadekang.tistory.com/6

 

[c++][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ [2022 KAKAO BLIND RECRUITMENT] https://programmers.co.kr/learn/courses/30/lessons/92334 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ ๋ฌธ์ œ ์„ค๋ช… ์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ..

wadekang.tistory.com

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments