DHistory

[Programmers] Level 1 - 카드 뭉치 본문

Computer Science/Algorithm

[Programmers] Level 1 - 카드 뭉치

ddu0422 2023. 6. 18. 23:21

문제

 

프로그래머스

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

programmers.co.kr

 

풀이

def solution(cards1, cards2, goal):
    # 초기화
    i = 0
    j = 0
    k = 0

    # 문제 풀이
    while True:
        # 종료 조건 1: Goal 완성 시 종료
        if k >= len(goal):
            return "Yes"
        
        current = goal[k]

        # 종료 조건 2: goal에 해당하는 card가 없는 경우 종료
        if cards1[i] != current and cards2[j] != current:
            return "No"

        if i < len(cards1) - 1 and cards1[i] == current:
            i += 1

        if j < len(cards2) - 1 and cards2[j] == current:
            j += 1

        k += 1

 

채점 결과