유형 : 스택? 구현
풀이 시간 : 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"
'프로그래머스 > Lv. 1' 카테고리의 다른 글
35. 2016년 (1) | 2024.07.14 |
---|---|
34. 폰켓몬 (set에 대하여) (0) | 2024.07.14 |
32. 콜라 문제 ★ (0) | 2024.07.14 |
31. 두 개 뽑아서 더하기 (itertools - combinations) (0) | 2024.07.14 |
30. 가장 가까운 같은 글자 (dict의 활용, 관련 함수들) (1) | 2024.07.14 |