프로그래머스/Lv. 1
33. 카드 뭉치
Seohyeong Lee
2024. 7. 14. 03:25
유형 : 스택? 구현
풀이 시간 : 10분
def solution(cards1, cards2, goal):
while True:
if cards1 and cards1[0] == goal[0]:
del cards1[0]
del goal[0]
if not goal:
return "Yes"
elif cards2 and cards2[0] == goal[0]:
del cards2[0]
del goal[0]
if not goal:
return "Yes"
else:
return "No"
보자마자 스택이 떠올랐다.
pop을 한 뒤, top 원소만 비교하고, 만약 일치하는 원소가 없다면 false.
끝까지 goal이 다 순회되면 true.
더 최적화한 코드.
def solution(cards1, cards2, goal):
while goal:
if cards1 and cards1[0] == goal[0]:
del cards1[0]
del goal[0]
elif cards2 and cards2[0] == goal[0]:
del cards2[0]
del goal[0]
else:
return "No"
return "Yes"