HANCO
[프로그래머스] 모의고사 (완전탐색) 본문
안녕하세요 박준영입니다.
프로그래머스 [모의고사] 문제를 풀어보았습니다.
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
vector<int> solution(vector<int> answers) {
vector<int> answer;
stack<int> st;
for(int i = answers.size()-1; i >= 0; i--){
st.push(answers[i]);
}
// 패턴 삽입
vector<int> m1 = {1, 2, 3, 4, 5};
vector<int> m2 = {2, 1, 2, 3, 2, 4, 2, 5};
vector<int> m3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
// 맞은 개수 초기값
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
int chk1 = 0, chk2 = 0, chk3 = 0;
// 값 비교
while(!st.empty()) {
if(m1[chk1] == st.top()) cnt1++;
if(m2[chk2] == st.top()) cnt2++;
if(m3[chk3] == st.top()) cnt3++;
chk1++, chk2++, chk3++;
if(chk1 == m1.size()) chk1 = 0;
if(chk2 == m2.size()) chk2 = 0;
if(chk3 == m3.size()) chk3 = 0;
st.pop();
}
vector<int> result;
result.push_back(cnt1);
result.push_back(cnt2);
result.push_back(cnt3);
int max_val = cnt1;
int max_index;
for(int i=0;i<result.size();i++){
if(max_val <= result[i]) {
max_val = result[i];
}
}
for(int i=0;i<result.size();i++){
if(max_val == result[i]) answer.push_back(i+1);
}
return answer;
}
반복문과 조건문만 잘 사용하면 풀리는 문제이지만
효율성 면에서 부족하다고 생각한다.
그렇기에 다음에는 이 코드를 더 다음어 봐야겠다.
'Algorithm > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 카펫 (완전탐색) (1) | 2020.10.06 |
---|---|
[프로그래머스] K번째 수 (정렬) (0) | 2020.09.29 |
[프로그래머스] 소수찾기 (완전탐색) (0) | 2020.09.29 |