프로그래머스 Lv.1 코딩테스트

[프로그래머스 Lv.1] 카드 뭉치

하다블 2023. 2. 17. 18:59
반응형

문제는 다음과 같습니다.

https://school.programmers.co.kr/learn/courses/30/lessons/159994?language=cpp 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

풀이 코드는 다음과 같습니다.

#include <string>
#include <vector>

using namespace std;

string solution(vector<string> cards1, vector<string> cards2, vector<string> goal) {
    string answer = "Yes";
    int idx{0},idx1{0},idx2{0};
    while(idx<=goal.size()-1)
    {
        if(goal[idx]==cards1[idx1])
        {
            idx++;
            idx1++;
        }
        else if(goal[idx]==cards2[idx2])
        {
            idx++;
            idx2++;
        }
        else
        {
            answer="No";
            break;
        }

    }
    
    return answer;
}

순서대로 카드를 사용해야 하므로 맨 처음 원소를 제거하면서 진행하거나 위 방식처럼 순서를 각각 측정하여 goal의 원소가 두 카드 뭉치에도 없으면 No를 출력하면서 break를 출력하도록 진행할 수 있습니다.

반응형