///
Search

카드 뭉치

항해 TIL

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

오답

from collections import deque def solution(cards1, cards2, goal): answer = '' cards1 = deque(cards1) cards2 = deque(cards2) temp = deque(goal) cnt = 0 while cards1 and cards2 and temp: t = temp.popleft() if t in (cards1[0], cards2[0]): if t == cards1[0]: answer += cards1.popleft() cnt += 1 elif t == cards2[0]: answer += cards2.popleft() cnt += 1 else: continue if cards1: while cards1: c = cards1.popleft() t = temp.popleft() if t == c: answer += c cnt += 1 if cards2: while cards2: c = cards2.popleft() t = temp.popleft() if t == c: answer += c cnt += 1 # print(answer) if len(goal) != cnt: return "No" return "Yes"
Python
복사
temp.popleft() 연산이 temp가 비어있을 때도 실행될 수 있습니다.
cards1이나 cards2가 비어있을 때 popleft() 연산을 수행할 수 있습니다.
문제를 제대로 안 읽음
2 ≤ goal의 길이 ≤ cards1의 길이 + cards2의 길이
goal이 card1과 card2보다 길기 때문에, goal 기준으로 card 1, 2 앞의 원소와 비교하면서 진행해야함

정답

def solution(cards1, cards2, goal): cards1 = deque(cards1) cards2 = deque(cards2) for word in goal: if cards1 and word == cards1[0]: cards1.popleft() elif cards2 and word == cards2[0]: cards2.popleft() else: return "No" return "Yes"
Python
복사
from collections import deque def solution(cards1, cards2, goal): answer = '' cards1 = deque(cards1) cards2 = deque(cards2) temp = deque(goal) while temp: t = temp.popleft() if cards1 and t == cards1[0]: answer += cards1.popleft() elif cards2 and t == cards2[0]: answer += cards2.popleft() else: return "No" return "Yes"
Python
복사
def solution(cards1, cards2, goal): answer = 'Yes' for i in goal: if i in cards1: if i ==cards1[0]: cards1.pop(0) else: answer='No' break elif i in cards2: if i == cards2[0]: cards2.pop(0) else: answer='No' break return answer
Python
복사