๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][์Šคํƒ/ํ] ํ”„๋กœ์„ธ์Šค

ibelieveinme 2023. 6. 4. 12:30
728x90

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ”„๋กœ์„ธ์Šค์˜ ์šฐ์„ ์ˆœ์œ„ ๊ฐ’์ด ๋“ค์–ด ์žˆ๋Š” priorities๋ฒกํ„ฐ์™€  ์•Œ๊ณ  ์‹ถ์€ ํ”„๋กœ์„ธ์Šค์˜ ์œ„์น˜์ธ location ๊ฐ’์ด ์ฃผ์–ด์ง„๋‹ค.

location ์œ„์น˜์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜๋Š” ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

*ํ’€์ด๋ฐฉ๋ฒ•

priorities ๊ธธ์ด๊ฐ€ 100 ๋ฐ–์— ์•ˆ๋˜๊ธธ๋ž˜, 100! ๋ฒˆ ์—ฐ์‚ฐํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ณ  ํ•œ์ค„์”ฉ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

๊น”๋”?ํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฑฐ ๊ฐ™๊ธดํ•œ๋ฐ... ์šฐ์„  ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ๋จผ์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ ๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋‹ˆ๊นŒ, ์—ฐ์‚ฐ์„ ์ค„์ด๋Š”๊ฒŒ ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฑฐ ๊ฐ™์•˜๋‹ค.

 

1) ๊ทธ๋ƒฅ ๋‹จ์ˆœํ•˜๊ฒŒ priorities ๋ฒกํ„ฐ๋ฅผ ์•ž์—์„œ ๋นผ๊ณ  ๋’ค๋กœ ๋„ฃ์œผ๋‹ˆ index๊ฐ€ ๊ณ„์† ๋ฐ”๊ปด์„œ location ๊ฐ’๋„ ๊ณ„์† ๋ณ€๊ฒฝ์ด ํ•„์š”ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ index๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ 0๋ถ€ํ„ฐ priorities.size()-1 ๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค.

2) ๋ฒกํ„ฐ์˜ max_element ํ•จ์ˆ˜๋กœ priorities ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ–๋Š” ํ”„๋กœ์„ธ์Šค์˜ ์ธ๋ฑ์Šค๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.

3) ์šฐ์„ ์ˆœ์œ„ ํ”„๋กœ์„ธ์Šค ์ „๊นŒ์ง€ ์•ž์˜ ๊ฐ’๋“ค์„ ๋‹ค ๋’ค๋กœ ๋ณด๋‚ธ๋‹ค.

4) ๋งจ ์•ž์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ ์ตœ์šฐ์„ ์ˆœ์œ„ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋นผ๋‚ธ๋‹ค.

5) ์ด ๋•Œ ๋นผ๋‚ธ ์ด ๊ฐ’์˜ ์ธ๋ฑ์Šค๊ฐ’์ด location๊ณผ ๊ฐ™๋‹ค๋ฉด ์šด์˜์ฒด์ œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  answer๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

*์•Œ์•„๋‘˜ ๊ฒƒ

1) ๋ฒกํ„ฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์˜ ์ธ๋ฑ์Šค ์•Œ์•„๋‚ด๊ธฐ

int max_index = max_element(v.begin(), v.end())-v.begin();

max_element๊ฐ€ ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ์•Œ๊ณ  ์‹ถ์œผ๋ฉด *max_element()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค!

 

2) ๋ฒกํ„ฐ์˜ ๋งจ ์•ž ๊ฐ’ ๋นผ๊ธฐ

v.erase(v.begin());

3) ๋ฒกํ„ฐ์˜ ๊ฐ€์žฅ ์•ž์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

v.front();

 

*์ฝ”๋“œ

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

int solution(vector<int> priorities, int location) {
    int answer = 0;
    int priority_index = 0;
    
    //์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฒกํ„ฐ ์ƒ์„ฑ
    vector<int> index_vector;
    for(int i = 0; i < priorities.size(); i++){
        index_vector.push_back(i);
    }
    
    //ํ”„๋กœ์„ธ์Šค ๊บผ๋‚ด๊ธฐ
    while(1){
        //๊ฐ€์žฅ ์ฒ˜์Œ ๋นผ๋‚ผ ์šฐ์„ ์ˆœ์œ„ ์ธ๋ฑ์Šค ์ €์žฅ
        priority_index = max_element(priorities.begin(), priorities.end())-priorities.begin();
        //์šฐ์„ ์ˆœ์œ„ ์ธ๋ฑ์Šค์˜ ๊ฐ’ ์ „๊นŒ์ง€ ๋‹ค ๋’ค๋กœ ๋ณด๋‚ด๊ธฐ
        for(int i = 0; i < priority_index; i++){
            priorities.push_back(priorities.front());
            priorities.erase(priorities.begin());

            //์ธ๋ฑ์Šค ๋ฒกํ„ฐ ๊ฐ’๋„ ๋’ค๋กœ ๋ณด๋‚ด๊ธฐ
            index_vector.push_back(index_vector.front());
            index_vector.erase(index_vector.begin());
        }
        //์šฐ์„ ์ˆœ์œ„ ์ธ๋ฑ์Šค ๋นผ๋‚ด๊ธฐ
        priorities.erase(priorities.begin());
        //๋นผ๋‚ผ๋•Œ๋งˆ๋‹ค ์นด์šดํŠธ ์„ธ๊ธฐ
        answer++;
        //๋นผ๋‚ธ ์ธ๋ฑ์Šค ๊ฐ’์ด location๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ
        if(location == index_vector.front()) break;
        //๋‹ค๋ฅด๋‹ค๋ฉด ์ธ๋ฑ์Šค ๋ฐฐ์—ด์—์„œ๋„ ๋งจ ์•ž ๊ฐ’ ๋นผ๊ณ  ๋‹ค์‹œ ํ”„๋กœ์„ธ์Šค ์ง„ํ–‰
        index_vector.erase(index_vector.begin());
    }

    return answer;
}
728x90