Algorithm/Programmers

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

Chae-ri🍒 2024. 6. 26. 01:02

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

 

프로그래머스

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

programmers.co.kr

 

def solution(cards1, cards2, goal):
    answer = ''
    inCards1Count = 0
    inCards2Count = 0
    subGoal = []
    for i in range(0, len(goal)):
        if(goal[i] in cards1) :
            subGoal.append(cards1[inCards1Count])
            inCards1Count += 1
        else :
            subGoal.append(cards2[inCards2Count])
            inCards2Count += 1
    if (goal == subGoal) :
        answer = "Yes"
    else :
        answer = "No"
    return answer

 

일단 첫 풀이는 이렇다...

뭔가 더 깔끔한 방법이 있을까 고민하다가 저렇게 제출했는데

 

똑똑하신 여러 개발자님들은 효율적인 코드를 대부분 짜주셨다...

def solution(cards1, cards2, goal):
    for g in goal:
        if len(cards1) > 0 and g == cards1[0]:
            cards1.pop(0)       
        elif len(cards2) >0 and g == cards2[0]:
            cards2.pop(0)
        else:
            return "No"
    return "Yes"

 

각 카드 배열의 길이가 0 이상인지 확인하고 goal 해당 문자열(g)와 각 카드 배열의 첫 요소가 같으면 pop 시키는 것!!!

 

더 넓은 사고를 가지고 접근해보자 아자아자

728x90