ornni 2024. 4. 19. 10:00
728x90
반응형

첫번째 코드

 

두 개의 리스트에서 하나씩 인덱스를 미뤄가면서 가능하면 답에 넣고 아니면 넣지 않는 방법으로 진행한다.

그러므로 투포인터를 사용해서 풀면 되겠구나!! 라고 생각했다.

 

def solution(cards1, cards2, goal):
    answer = ""
    index1 = 0
    index2 = 0
    A = []

    for i in goal:
        if i == cards1[index1]:
            A.append(cards1[index1])
            if index1 < (len(cards1) - 1):
                index1 += 1

        elif i == cards2[index2]:
            A.append(cards2[index2])
            if index2 < (len(cards2) - 1):
                index2 += 1
        else:
            break

    if A == goal:
        answer += "Yes"
    else:
        answer += "No"
    return answer

 

통과!

나름 이제 배웠던 것도 잘 써먹고 뿌듯하다 나 자신:)


링크

https://github.com/ornni/programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/1/159994.%E2%80%85%EC%B9%B4%EB%93%9C%E2%80%85%EB%AD%89%EC%B9%98

 

programmers/프로그래머스/1/159994. 카드 뭉치 at main · ornni/programmers

repository for recording Programmers Algorithm problem solving - ornni/programmers

github.com

 

반응형