https://programmers.co.kr/learn/courses/30/lessons/42840
๋์ถฉ ํ๋ฉด ์ฌ์ด ๋ฌธ์ . ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ณ ๋ช ํํ๊ฒ ์ฐ๋ ๊ฑธ ์ฐ์ตํ์.
์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
int size = answers.size();
int correct[3] = { 0,0,0 };
int secondList[4] = { 1,3,4,5 };
int thirdList[5] = { 3,1,2,4,5 };
for (int i = 0; i<size; i++) {
int ans = answers[i];
if ((i % 5) + 1 == ans) // 1๋ฒ
correct[0]++;
if (i % 2 == 0 && ans == 2) // 2๋ฒ
correct[1]++;
else if (i % 2 != 0 && secondList[(i / 2) % 4] == ans)
correct[1]++;
if (thirdList[(i / 2) % 5] == ans) // 3๋ฒ
correct[2]++;
}
if (correct[0]>correct[1]) {
if (correct[1] >= correct[2]) {
answer.push_back(1);
}
else {
if (correct[2]>correct[0])
answer.push_back(3);
else if (correct[2] == correct[0]) {
answer.push_back(1);
answer.push_back(3);
}
else {
answer.push_back(1);
}
}
}
else if (correct[0] == correct[1]) {
if (correct[0]>correct[2]) {
answer.push_back(1);
answer.push_back(2);
}
else if (correct[0] == correct[2]) {
answer.push_back(1);
answer.push_back(2);
answer.push_back(3);
}
else {
answer.push_back(3);
}
}
else {
if (correct[1]<correct[2]) {
answer.push_back(3);
}
else if (correct[1]>correct[2])
answer.push_back(2);
else {
answer.push_back(2);
answer.push_back(3);
}
}
return answer;
}
๋ฐ์ ์ํฌ ์
+) max_element
๋ผ๋ ํจ์๋ฅผ ์ฐ๋ฉด ๊น๋ํจ
<algorithm> header file์ ์์ผ๋ฉฐ (์์์ฃผ์, ๋์ฃผ์)๋ฅผ ๋๊ธฐ๋ฉด max ๊ฐ์ ์ฐพ์์ ๋๊ฒจ์ค
+) ํจํด์ ์ฌ๋ฌ๊ฐ๋ฅผ ๋ฐฐ์ด์ ํ๊บผ๋ฒ์ ๋ฃ๊ณ ํ๋ฉด ๊น๋ํจ
๋ฐ์ํ
'Algorithm ๋ฌธ์ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค :: ํ (๋ฌธ์ ํ์ด, ์ฝ๋) (0) | 2020.07.26 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค :: ์นดํซ (brute force) (0) | 2020.05.28 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค :: ์ซ์์ผ๊ตฌ (brute force) (0) | 2020.05.28 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค :: ์์ ์ฐพ๊ธฐ (brute force) (0) | 2020.05.24 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค :: N์ผ๋ก ํํ (๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ) (0) | 2020.04.02 |
Comment