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

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

WonderJay 2022. 7. 2. 00:53
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

์ˆ˜๋งŽ์€ ๋งˆ๋ผํ†ค ์„ ์ˆ˜๋“ค์ด ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ ํ•œ ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant์™€ ์™„์ฃผํ•œ ์„ ์ˆ˜

programmers.co.kr

์ฐธ๊ฐ€์ž ๋ฐฐ์—ด์€ ์™„์ฃผ์ž ๋ฐฐ์—ด๋ณด๋‹ค ํ•ญ์ƒ ๊ธธ์ด๊ฐ€ 1 ๋งŒํผ ๋” ๊ธธ๋‹ค๋Š” ์ ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

๋จผ์ € ๋‘ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ๋‹ค์Œ, ๋’ท ๋ถ€๋ถ„์˜ ์›์†Œ๋“ค์„ ๋น„๊ตํ•œ๋‹ค.

๋งŒ์•ฝ์— ์„œ๋กœ ๊ฐ™์œผ๋ฉด ๊ฐ๊ฐ pop_back() ์„ ํ˜ธ์ถœํ•œ๋‹ค.

๋™๋ช…์ด์ธ์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ์™„์ฃผ์ž ๋ฐฐ์—ด์€ ํ……ํ…… ๋น„๊ณ  ์ฐธ๊ฐ€์ž ๋ฐฐ์—ด์—๋Š” 1๊ฐœ์˜ ์›์†Œ๋งŒ ๋‚จ๋Š”๋ฐ, ๊ทธ๊ฒƒ์ด ๋‹ต์ด๋‹ค.

๋งŒ์•ฝ ๋™๋ช…์ด์ธ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ˜๋“œ์‹œ back ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋•Œ์˜ participant ์˜ ์›์†Œ๊ฐ€ ๋ฐ”๋กœ ๋‹ต์ด๋‹ค.

 

 

 

[C++]

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";

    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());

    for (int i = 0; i < participant.size(); i++)
    {
        for (int j = 0; j < completion.size(); j++)
        {
            if (participant.back() == completion.back())
            {
                participant.pop_back();
                completion.pop_back();
            }
            else
            {
                answer = participant.back();
                break;
            }
        }
    }
    if (answer.empty())
        answer = participant.back();

    return answer;
}


ํ˜น์€ ํ•ด์‰ฌ๋งต์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜๋„ ์žˆ๋‹ค.

<string, int> ์™€ ๊ฐ™์€ key - value record ๋ฅผ ๊ฐ€์ง€๋Š” unordered_map ์„ ์„ ์–ธํ•œ ๋‹ค์Œ, ํ•ด๋‹น ํ•ด์‰ฌ๋งต์— completion ์˜ ์›์†Œ๋ฅผ

insert ํ•œ๋‹ค. completion ์˜ ์›์†Œ๊ฐ€ ํ•ด์‰ฌ๋งต์— ์—†๋‹ค๋ฉด <์ด๋ฆ„, 1> ์˜ ํ˜•ํƒœ๋กœ ๋„ฃ๋Š”๋‹ค. ์ด๋ฏธ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น record ์˜ int ๊ฐ’์„ ์ฆ๊ฐ€ํ•ด์ค€๋‹ค. (count ์—ญํ• )

์ดํ›„ participant ์˜ ์›์†Œ๊ฐ€ hashmap ์— ์žˆ๋Š”์ง€ check ํ•œ๋‹ค. ์—†๋‹ค๋ฉด ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ฐ”๋กœ ๋‹ต์ด๋‹ค. ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์›์†Œ์˜ count ๊ฐ’์„ ๊ฐ์†Œ์‹œํ‚จ๋‹ค. ์ดํ›„, hash_map ์˜ ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ count ๊ฐ’์ด ์Œ์ˆ˜์ธ ์›์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ ๋‹ต์ด๋‹ค.

 

[c++]

#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";

    unordered_map<string, int> hash_map;

    for (auto& ele : completion)
    {
        if (hash_map.find(ele) == hash_map.end())
        {
            hash_map.insert(make_pair(ele, 1));
        }
        else
            hash_map[ele]++;
    }
    for (auto& ele : participant)
    {
        if (hash_map.find(ele) == hash_map.end())
        {
            // can't find
            answer = ele;
            break;
        }
        else
        {
            hash_map[ele]--;
            if (hash_map[ele] < 0)
            {
                answer = ele;
                break;
            }
        }
    }

    return answer;
}

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