가을맛 2021. 8. 20. 23:22
문제
코딩테스트 연습 - 모의고사
https://programmers.co.kr/learn/courses/30/lessons/42840

리뷰

  • 총 소요시간 : 40분(정답)

    난이도에 비해 구현이 너무 오래걸렸다.

    모범답안

    int they_max = *max_element(they.begin(),they.end());

    이부분이 진짜 예술이다..

    max_element는 iter를 리턴하므로 *을 붙여서 그 값을 int they_max에 저장하도록 한다.

     

    #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;
    }