๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][์™„์ „ํƒ์ƒ‰] ๋ชจ์˜๊ณ ์‚ฌ

ibelieveinme 2023. 4. 21. 23:23
728x90

[๋ฌธ์ œ]

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

 

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

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

programmers.co.kr

 

[ํ•ด๊ฒฐ์ฑ…]

1. 1,2,3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ํ‘œ๊ธฐํ•œ ๋‹ต์„ ๋ฒกํ„ฐ์— ์ €์žฅํ•ด๋‘”๋‹ค.

2. 1,2,3๋ฒˆ ์ˆ˜ํฌ์ž์˜ ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฒกํ„ฐ๋ฅผ ์„ ์–ธํ•œ๋‹ค. vector<int> answerNum(3);

3. answer์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ฒดํฌํ•œ๋‹ค.

    ์ด ๋•Œ, ์ˆ˜ํฌ์ž์˜ ๋‹ต ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๊ณ  ์ •๋‹ต ๊ฐœ์ˆ˜ ์ดํ•˜์ด๋ฏ€๋กœ ๋‚˜๋จธ์ง€ ๊ณ„์‚ฐ์œผ๋กœ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•œ๋‹ค.

4. ์ตœ๋Œ€ ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์•„์„œ answerNum ๋ฒกํ„ฐ์— ๋„ฃ์–ด์ค€๋‹ค.(์ค‘๋ณต๊ฐ€๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ์œผ๋กœ)

 

[์•Œ์•„๋‘˜ ๊ฒƒ]

*๋ฒกํ„ฐ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”

vector answerNum(3);
vector<int> person1 = {1, 2, 3, 4, 5};

 

*๋ฒกํ„ฐ ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

int max = *max_element(v.begin(), v.end());
cout << "๊ฐ€์žฅ ํฐ ์ˆ˜ : " << max << "\n";
    
int max_index = max_element(v.begin(), v.end()) - v.begin();
cout << "๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ธ๋ฑ์Šค : " << max_index;

 

[์ฝ”๋“œ]

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

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> person1 = {1, 2, 3, 4, 5}; //5
    vector<int> person2 = {2, 1, 2, 3, 2, 4, 2, 5}; //8
    vector<int> person3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; //10
    vector<int> answerNum(3);
    
    //1. person๋“ค ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฉด์„œ answer๊ฐ’์ด๋ž‘ ๋น„๊ตํ•˜๊ธฐ
    //2. ์‚ฌ์ด์ฆˆ๊ฐ€ ๋‹ค๋ฅด๋‹ˆ๊นŒ answer ๊ธธ์ด๋งŒํผ ๊ณ„์† size๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ index๋กœ ์ด๋™ํ•˜๋ฉฐ ๋ฐ˜๋ณต
    for(int i = 0; i < answers.size(); i++){
        if(person1[i%person1.size()] == answers[i]) answerNum[0]++;
        if(person2[i%person2.size()] == answers[i]) answerNum[1]++;
        if(person3[i%person3.size()] == answers[i]) answerNum[2]++;
    }
    
    //3. ์ œ์ผ ๋งŽ์ด ๋งž์ถ˜ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
    int maxValue = max(answerNum[0], max(answerNum[1], answerNum[2]));
    if(answerNum[0] == maxValue){
        answer.push_back(1);
    }
    if(answerNum[1] == maxValue){
        answer.push_back(2);
    }
    if(answerNum[2] == maxValue){
        answer.push_back(3);
    }
             
    return answer;
}

์œ„ ์ฝ”๋“œ ๊ฐœ์„ 

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

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> person1 = {1, 2, 3, 4, 5}; //5
    vector<int> person2 = {2, 1, 2, 3, 2, 4, 2, 5}; //8
    vector<int> person3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}; //10
    vector<int> answerNum(3);
    
    //1. person๋“ค ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฉด์„œ answer๊ฐ’์ด๋ž‘ ๋น„๊ตํ•˜๊ธฐ
    //2. ์‚ฌ์ด์ฆˆ๊ฐ€ ๋‹ค๋ฅด๋‹ˆ๊นŒ answer ๊ธธ์ด๋งŒํผ ๊ณ„์† size๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ index๋กœ ์ด๋™ํ•˜๋ฉฐ ๋ฐ˜๋ณต
    for(int i = 0; i < answers.size(); i++){
        if(person1[i%person1.size()] == answers[i]) answerNum[0]++;
        if(person2[i%person2.size()] == answers[i]) answerNum[1]++;
        if(person3[i%person3.size()] == answers[i]) answerNum[2]++;
    }
    
    //3. ์ œ์ผ ๋งŽ์ด ๋งž์ถ˜ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
    int maxValue = *max_element(answerNum.begin(), answerNum.end());
    for(int i = 0; i <= answerNum.size(); i++){
        if(answerNum[i] == maxValue) answer.push_back(i+1);
    }
             
    return answer;
}
728x90