Notice
Recent Posts
Recent Comments
Today
Total
01-11 05:49
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ ๋ณธ๋ฌธ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ

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

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ์˜๊ณ ์‚ฌ

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š”

programmers.co.kr

< ์ˆ˜ํฌ์ž1 , ๋งž์€ ์ ์ˆ˜ > , < ์ˆ˜ํฌ์ž2 , ๋งž์€ ์ ์ˆ˜ > , < ์ˆ˜ํฌ์ž3, ๋งž์€ ์ ์ˆ˜ >  ์™€ ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๋Š”

ํ•ด์‰ฌ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ˆ˜ํฌ์ž1, 2, 3 ์ด ๋ฌธ์ œ๋ฅผ ์ฐ๋Š” ๋ฐฉ์‹์€ ๊ฐ๊ฐ 5, 8, 10 ์˜ ์ˆœํ™˜๋งˆ๋””๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ด์— ๋”ฐ๋ผ์„œ

๊ฐ๊ฐ ๋งž์€ ์ ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•ด์‰ฌ ํ…Œ์ด๋ธ”์„ key ๊ฐ€ ์•„๋‹Œ value ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ sort ํ•ด์ฃผ๋ฉด ํŽธํ•œ๋ฐ

์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•ด์‰ฌ ํ…Œ์ด๋ธ”์˜ record ๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง€๋Š” vector ์„ ์„ ์–ธํ•œ ๋’ค sorting ์„ ํ•ด์ค€๋‹ค.

vector ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์—ฐ์†์ ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์ •๋ ฌ์„ ๋งˆ์น˜๋ฉด v[0], v[1], v[2] ๋Š” ๊ฐ๊ฐ ๊ณ ๋“์ ํ•œ ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ๋‹ค. (compare ํ•จ์ˆ˜ ์ฃผ์˜)

1๋“ฑ์ด ๋ช‡๋ช…์ธ์ง€์— ๋”ฐ๋ผ answer ์— ๋ˆ„๊ตฌ๋ฅผ insert ํ•˜๊ณ  ์ •๋ ฌ์„ ํ• ์ง€ ๋ง์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

v[0].second ๊ฐ€ v[1] ๋ณด๋‹ค ํฌ๋ฉด ๋‹จ๋… 1๋“ฑ์ด ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋ฏ€๋กœ v[0].first ๋งŒ push ํ•ด์ฃผ๊ณ  ๋๋‚ธ๋‹ค.

 

v[0].second ๊ฐ€ v[1].second ์™€ ๊ฐ™๊ณ  v[1].second ๊ณผ v[2].second ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด 1๋“ฑ์ด 2๋ช… ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋ฏ€๋กœ,

v[0].first ์™€ v[1].first ๋ฅผ push ํ•œ ๋’ค์— ์ •๋ ฌํ•˜๊ณ  ๋๋‚ธ๋‹ค.

 

v[0].second ์™€ v[1].second ์™€ v[2].second ๊ฐ€ ๋ชจ๋‘ ๊ฐ™๋‹ค๋ฉด 1๋“ฑ์ด 3๋ช… ๋ฐœ์ƒํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ฐ๊ฐ์˜ first ๋ฅผ push ํ•˜๊ณ 

์ •๋ ฌํ•œ ๋’ค ๋๋‚ธ๋‹ค.

 

 

 

[C++]

#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>

using namespace std;
 
int one[] = { 1, 2, 3, 4, 5 }; // 5
int two[] = { 2, 1, 2, 3, 2, 4, 2, 5 }; // 8
int three[] = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 }; // 10

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    int num = answers.size();

    unordered_map <int, int> idx;
    idx.insert(make_pair(1, 0));
    idx.insert(make_pair(2, 0));
    idx.insert(make_pair(3, 0));

    int ans = 0;
    for (int i = 0; i < num; i++)
    {
        ans = answers[i];
        if (ans == one[i % 5])
        {
            idx[1]++;
        }
        if (ans == two[i % 8])
        {
            idx[2]++;
        }
        if (ans == three[i % 10])
        {
            idx[3]++;
        }
    }

    vector<pair<int, int>> v(idx.begin(), idx.end());

    sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b)
        {
            return a.second > b.second;
        });


    if (v[0].second > v[1].second)
    {
        answer.push_back(v[0].first);
        return answer;
    }
    if (v[0].second == v[1].second && v[1].second != v[2].second)
    {
        answer.push_back(v[0].first);
        answer.push_back(v[1].first);
        sort(answer.begin(), answer.end());
        return answer;
    }
    if (v[0].second == v[1].second && v[1].second == v[2].second)
    {
        answer.push_back(v[0].first);
        answer.push_back(v[1].first);
        answer.push_back(v[2].first);
        sort(answer.begin(), answer.end());
    }
    return answer;
}

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