728x90
[๋ฌธ์ ]
https://school.programmers.co.kr/learn/courses/30/lessons/42840
[ํด๊ฒฐ์ฑ ]
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
'๐ Coding Test Study > Algorithm Problem' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++][Programmers][์์ ํ์] ํผ๋ก๋ (0) | 2023.04.26 |
---|---|
[C++][Programmers][์์ ํ์] ์นดํซ (0) | 2023.04.24 |
[C++][Programmers][์์ ํ์] ์ต์์ง์ฌ๊ฐํ (0) | 2023.04.21 |
[C++][Programmers][์ ๋ ฌ] H-Index (0) | 2023.04.20 |
[C++][Programmers][์ ๋ ฌ] ๊ฐ์ฅ ํฐ ์ (1) | 2023.04.19 |