~๋ชฉ์ฐจ~
๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/43164
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
- ํฐ์ผ์ ํ ๋ฒ๋ง ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก ์ฌ์ฉํ์์ ๊ธฐ๋กํด์ผํจ (ํ์ง๋ง, ์ค๋ณต์ด ์์ ์ ์์)
- ํน์ ๊ณตํญ์์ ๊ฐ ์ ์๋ ๊ณตํญ์ ์ฌ๋ฆผ์ฐจ์์ผ๋ก ๊ด๋ฆฌ
- DFS๋ก ์ํํ๋ฉด์ ์ ๋ต์ด ๋ ์ ์๋ ์ฒซ๋ฒ์งธ ๊ฒฝ์ฐ๋ฅผ ๋ฐํ
- ์ฃผ์ ์ฌํญ
1. ํฐ์ผ์ ํ ๋ฒ๋ง ์ฌ์ฉํ ์ ์์ง๋ง, ๋์ผํ ๊ฒฝ๋ก์ ํฐ์ผ์ด 2๊ฐ ์ด์์ผ ์ ์์ ( 2๋ฒ ํ ์ผ)
2. ํน์ ๊ณตํญ์์ ๋ค๋ฅธ ๊ณตํญ์ผ๋ก ๊ฐ ์ ์๋๋ฐ ํด๋น ๊ณตํญ์ ์ ์ฅํ์ง ์์๋ค๋ฉด ๋ฐํ์์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ ( 1๋ฒ ํ ์ผ )
- ์ถ๊ฐํด ๋ณผ ํ ์ผ
์ ๋ ฅ | ๊ฒฐ๊ณผ |
[["ICN", "BOO"], ["ICN", "COO"], ["COO", "DOO"], ["DOO", "COO"], ["BOO", "DOO"], ["DOO", "BOO"], ["BOO", "ICN"], ["COO", "BOO"]] | ["ICN", "BOO", "DOO", "BOO", "ICN", "COO", "DOO", "COO", "BOO"] |
[["ICN", "BBB"], ["BBB", "ICN"], ["ICN", "AAA"]] | ["ICN", "BBB", "ICN", "AAA"] |
์์ฑ ์ฝ๋
def solution(tickets):
route = ['' for _ in range(len(tickets)+1)]
ticket = {}
used = {}
for t1, t2 in tickets:
ticket[t1] = ticket.get(t1, []) + [t2]
used[(t1, t2)] = used.get((t1, t2), 0) + 1
for t in ticket:
ticket[t].sort()
def dfs(count, city):
route[count] = city
if count == len(tickets):
return True
if city in ticket:
for c in ticket[city]:
if used[(city, c)] > 0:
used[(city, c)] -= 1
if dfs(count+1, c):
return True
used[(city, c)] += 1
return False
dfs(0, "ICN")
return route
๋์์ด ๋์ จ๋ค๋ฉด ์ข์์ ๋๋ฌ์ฃผ์ธ์๐
'๐๋ฌธ์ ํ์ด > ๐งฉProgrammers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ํ๋ก๊ทธ๋๋จธ์ค ์ ํ์ ์๊ฐ ์ด๋ (0) | 2024.06.27 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ํ๋ ธ์ด์ ํ | ์ฌ๊ท ํธ์ถ | ๋ถํ ์ ๋ณต (0) | 2023.12.25 |
[Python] ํ๋ก๊ทธ๋๋จธ์ค ๊ดํธ ๋ณํ | DFS (0) | 2023.06.27 |
[Python] ํ๋ก๊ทธ๋๋จธ์ค ํผ๋ณด๋์น ์ | DP (0) | 2023.06.26 |
[Python] ํ๋ก๊ทธ๋๋จธ์ค ๋ค์ ํฐ ์ซ์ (0) | 2023.06.26 |