#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(pair<int, int> a, pair<int,int> b) {
return a.second > b.second;
}
vector<int> solution(vector<int> answers) {
vector<int> answer;
vector<pair<int,int>> scores = {{1, 0}, {2, 0}, {3, 0}};
// 1번 수포자
vector<int> p1 = {1, 2, 3, 4, 5};
// 2번 수포자
vector<int> p2 = {2, 1, 2, 3, 2, 4, 2, 5};
// 3번 수포자
vector<int> p3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
for (int i = 0; i < answers.size(); i++) {
if (answers[i] == p1[i % p1.size()]) {
scores[0].second++;
}
if (answers[i] == p2[i % p2.size()]) {
scores[1].second++;
}
if (answers[i] == p3[i % p3.size()]) {
scores[2].second++;
}
}
sort(scores.begin(), scores.end(), compare);
if (scores[0].second == scores[1].second) {
if (scores[1].second == scores[2].second) {
answer = {1, 2, 3};
}
else {
answer = {scores[0].first, scores[1].first};
}
}
else {
answer = {scores[0].first};
}
return answer;
}
💭 풀이
1. 정답 배열과 각 수포자의 답 배열을 비교한다.
2. 정답과 일치하는 수포자의 점수를 1씩 증가시킨다.
3. <수포자 번호, 점수> 배열을 점수를 기준으로 정렬한다.
4. 셋 다 점수가 같을 경우, 두 명의 점수가 같을 경우, 그 외 (한 명만 최고점)의 경우에 따라 answer을 반환한다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> one = {1,2,3,4,5};
vector<int> two = {2,1,2,3,2,4,2,5};
vector<int> thr = {3,3,1,1,2,2,4,4,5,5};
vector<int> solution(vector<int> answers) {
vector<int> answer;
vector<int> they(3);
for(int i=0; i<answers.size(); i++) {
if(answers[i] == one[i%one.size()]) they[0]++;
if(answers[i] == two[i%two.size()]) they[1]++;
if(answers[i] == thr[i%thr.size()]) they[2]++;
}
int they_max = *max_element(they.begin(),they.end());
for(int i = 0; i< 3; i++) {
if(they[i] == they_max) answer.push_back(i+1);
}
return answer;
}
💡 알게된 점
max_element()를 사용하면 최대값의 인덱스를 구할 수 있다.- 가장 큰 값을 먼저 구하고, 해당 최대값을 가진 요소들의 인덱스를 반환하면 된다.