[프로그래머스/C++] 2의 영역

꿈별·2023년 8월 28일
0

문제풀이

목록 보기
33/52

문제


풀이

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr) {
    vector<int> answer;
    vector<int> count;

    for (int i = 0; i < arr.size(); ++i)
    {
        if (2 == arr[i])
        {
            count.push_back(i);
        }
    }

    if (0 == count.size())
    {
        answer.push_back(-1);
    }

    else if (1 == count.size())
    {
        answer.push_back(2);
    }

    else // 2개 이상
    {
        sort(count.begin(), count.end());
        for (int i = count[0]; i <= count[count.size()-1]; ++i)
        {
            answer.push_back(arr[i]);
        }

    }
    return answer;
}

0개의 댓글