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

2023. 4. 12. 13:19코딩연습

1. 문제

https://school.programmers.co.kr/learn/courses/30/lessons/159994

 

프로그래머스

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

programmers.co.kr

 

2. 풀이

- 각각의 카드 뭉치에서 한장씩 사용이 가능하고, 순서대로 꺼내서만 사용이 가능하다. 그러므로 카드뭉치의 첫번째 값과 목표의 값들과 비교를 진행하면 될 것이라고 생각했다.

function solution(cards1, cards2, goal) {
    const [copyCards1, copyCards2] = [cards1, cards2];
    
    let chkAnswer = true;
    let answer = ""
    for(let item of goal){
        if(item === copyCards1[0]){
            copyCards1.shift();
        } else if(item === copyCards2[0]){
            copyCards2.shift();
        } else if(item !== copyCards1[0] && item !== copyCards2[0]){
            chkAnswer = false;
        }
        
        if(!chkAnswer) break;
    }
    
    if(chkAnswer) return "Yes"
    else return "No"
}