<프로그래머스 문제풀이>
해답)
from collections import defaultdict
def solution(tickets):
START = "ICN"
citiesDests = defaultdict(list)
for ticket in tickets: # 한도시당 edge 수를 계산
citiesDests[ticket[0]].append(ticket[1])
for city in citiesDests.keys():
citiesDests[city].sort()
# print(citiesDests)
stack = []
res = []
def Go(start):
stack = []
stack.append(start)
if len(citiesDests[start]) == 0:
# print("현재 stack => ", stack)
# print("here, start=>", start)
top = stack.pop()
# print("변화 stack => ", stack)
# print("append top => ", top)
res.append(top)
return
for city in citiesDests[start][:]:
if city not in citiesDests[start]:
continue
citiesDests[start].remove(city)
Go(city)
while stack:
res.append(stack.pop())
Go(START)
res.reverse()
return res
풀이)
Stack 을 이용한 DFS 풀이 입니다.
* 이 문제 및 로고의 저작권은 Programmers에 있습니다.
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
728x90
'문제풀이 > Programmers' 카테고리의 다른 글
[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 두 정수 사이의 합 / Python 문제풀이 (0) | 2020.03.12 |
---|---|
[프로그래머스💯] 코딩테스트 연습 > 이분탐색 > 입국심사 / Python 문제풀이 (0) | 2020.03.09 |
[프로그래머스💯] 코딩테스트 연습 > 동적 계획법 > 카드 게임 (0) | 2020.03.02 |
[프로그래머스💯] 코딩테스트 연습 > 동적 계획법 > N으로 표현 (JavaScript) (0) | 2020.02.28 |
[프로그래머스💯][JS] 코딩테스트 연습 > 탐욕법(Greedy) > 섬 연결하기 (0) | 2020.02.09 |
댓글