728x90
๋ฐ์ํ
๋ฌธ์ ์ค๋ช
์ฃผ์ด์ง ํญ๊ณต๊ถ์ ๋ชจ๋ ์ด์ฉํ์ฌ ์ฌํ๊ฒฝ๋ก๋ฅผ ์ง๋ ค๊ณ ํฉ๋๋ค. ํญ์ "ICN" ๊ณตํญ์์ ์ถ๋ฐํฉ๋๋ค.
ํญ๊ณต๊ถ ์ ๋ณด๊ฐ ๋ด๊ธด 2์ฐจ์ ๋ฐฐ์ด tickets๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ๋ฐฉ๋ฌธํ๋ ๊ณตํญ ๊ฒฝ๋ก๋ฅผ ๋ฐฐ์ด์ ๋ด์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- ๋ชจ๋ ๊ณตํญ์ ์ํ๋ฒณ ๋๋ฌธ์ 3๊ธ์๋ก ์ด๋ฃจ์ด์ง๋๋ค.
- ์ฃผ์ด์ง ๊ณตํญ ์๋ 3๊ฐ ์ด์ 10,000๊ฐ ์ดํ์ ๋๋ค.
- tickets์ ๊ฐ ํ [a, b]๋ a ๊ณตํญ์์ b ๊ณตํญ์ผ๋ก ๊ฐ๋ ํญ๊ณต๊ถ์ด ์๋ค๋ ์๋ฏธ์ ๋๋ค.
- ์ฃผ์ด์ง ํญ๊ณต๊ถ์ ๋ชจ๋ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- ๋ง์ผ ๊ฐ๋ฅํ ๊ฒฝ๋ก๊ฐ 2๊ฐ ์ด์์ผ ๊ฒฝ์ฐ ์ํ๋ฒณ ์์๊ฐ ์์๋ ๊ฒฝ๋ก๋ฅผ return ํฉ๋๋ค.
- ๋ชจ๋ ๋์๋ฅผ ๋ฐฉ๋ฌธํ ์ ์๋ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
tickets | return |
[["ICN", "JFK"], ["HND", "IAD"], ["JFK", "HND"]] | ["ICN", "JFK", "HND", "IAD"] |
[["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL","SFO"]] | ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"] |
์ ์ถ๋ ฅ ์ ์ค๋ช
์์ #1
["ICN", "JFK", "HND", "IAD"] ์์ผ๋ก ๋ฐฉ๋ฌธํ ์ ์์ต๋๋ค.
์์ #2
["ICN", "SFO", "ATL", "ICN", "ATL", "SFO"] ์์ผ๋ก ๋ฐฉ๋ฌธํ ์๋ ์์ง๋ง ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"] ๊ฐ ์ํ๋ฒณ ์์ผ๋ก ์์ญ๋๋ค.
ํ์ด
- ์ด๊ฒ๋ DFS/BFS ๋ฌธ์ ๋ค.
- ๊ทผ๋ฐ ์ด์ฐจํผ
- ๋ชจ๋ ๋์๋ฅผ ๋ฐฉ๋ฌธํ ์ ์๋ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์๋๋ค.
- ์ธ์ฒ์์ ์ถ๋ฐํ๋ค.
- ๊ทธ๋ผ ๊ฒฐ๊ตญ ์๋ฌด๋ ๊ฒ๋ ๊ฐ๋ ๋๋ค๋ ๊ฑฐ ์๋๊ฐ?
- ๊ทผ๋ฐ ์ด์ฐจํผ
- ์ด... ์ ์๋๋ค /์งฌ๊น๋ง ใ
ใ
ใ
- ๋ค์ ์๊ฐํด๋ณด์....
- ์ด๊ฑฐ ... ์...
- ๊ทธ๋ฅ ์๋ฌด๋ ๊ฒ๋ ํ์ด๋ณด๋ค๊ฐ ์๋์ ๋ค์ DFS๋ BFS ์ง๋ฉด ์๊ฐ ๋ญ๋น ์๋๋ ใ ใ
- ํ์ง๋ง ๋ ๋ง์์ด ๋๋ฆฌ๋๋๋ก ๋ฌด์ง์ฑ ์ฝ๋ฉ์ ํ๋ค. ใ ใ ใ
- ์ด๊ฑฐ ... ์...
- ๋ค์ ์๊ฐํด๋ณด์....
- ์์ ํด๋ฒ๋ฆฌ๋ฉด ๋์ง?
- ์๋๋ค?
- ๋๋ฒ๊ทธ ์์ํ ๋ค ์ฐ์ด๋ด๋ ๋ชจ๋ฅด๊ฒ ๋ค?
- ์๋๋ค?
def solution(tickets):
answer = []
cities = sum(tickets, []) # ํ๋ฒ์ ํฉ์ณ์ค๋ค.
cities = list(set(cities))
cities.sort()
city_number = {}
for i in range(len(cities)):
city_number[cities[i]] = i
ticket_matrix = [[0 for _ in range(len(cities))] for _ in range(len(cities))]
for t in tickets:
ticket_matrix[city_number[t[0]]][city_number[t[1]]] += 1
travel_count = len(tickets) + 1
city = city_number["ICN"]
print(cities)
print(city_number)
print(ticket_matrix)
answer.append(cities[city])
for i in range(travel_count):
for i in range(len(cities)):
if ticket_matrix[city][i] != 0:
ticket_matrix[city][i] -= 1
city = i
answer.append(cities[city])
break
print(cities)
print(city_number)
print(ticket_matrix)
print(answer)
return answer
- ํ
์คํธ ์ผ์ด์ค
- ๋ค ๋์์...?
ํ
์คํธ 1
์
๋ ฅ๊ฐ ใ [["ICN", "JFK"], ["HND", "IAD"], ["JFK", "HND"]]
๊ธฐ๋๊ฐ ใ ["ICN", "JFK", "HND", "IAD"]
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
์ถ๋ ฅ ใ ['HND', 'IAD', 'ICN', 'JFK']
{'HND': 0, 'IAD': 1, 'ICN': 2, 'JFK': 3}
[[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0]]
['HND', 'IAD', 'ICN', 'JFK']
{'HND': 0, 'IAD': 1, 'ICN': 2, 'JFK': 3}
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
['ICN', 'JFK', 'HND', 'IAD']
ํ
์คํธ 2
์
๋ ฅ๊ฐ ใ [["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL", "SFO"]]
๊ธฐ๋๊ฐ ใ ["ICN", "ATL", "ICN", "SFO", "ATL", "SFO"]
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
์ถ๋ ฅ ใ ['ATL', 'ICN', 'SFO']
{'ATL': 0, 'ICN': 1, 'SFO': 2}
[[0, 1, 1], [1, 0, 1], [1, 0, 0]]
['ATL', 'ICN', 'SFO']
{'ATL': 0, 'ICN': 1, 'SFO': 2}
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
['ICN', 'ATL', 'ICN', 'SFO', 'ATL', 'SFO']
ํ
์คํธ 3
์
๋ ฅ๊ฐ ใ [["ICN", "A"], ["A", "B"], ["A", "C"], ["B", "A"], ["C", "A"]]
๊ธฐ๋๊ฐ ใ ["ICN", "A", "B", "A", "C", "A"]
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
์ถ๋ ฅ ใ ['A', 'B', 'C', 'ICN']
{'A': 0, 'B': 1, 'C': 2, 'ICN': 3}
[[0, 1, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
['A', 'B', 'C', 'ICN']
{'A': 0, 'B': 1, 'C': 2, 'ICN': 3}
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
['ICN', 'A', 'B', 'A', 'C', 'A']
- ํ์ง๋ง ์คํจ
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ์คํจ (0.09ms, 10.3MB)
ํ
์คํธ 2 ใ ์คํจ (0.04ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.03ms, 10.4MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.1MB)
- ํ ์ฐธ ์ฝ๋ค๋ณด๋ ์ด๊ฒ ํจ์ ์ธ ๊ฒ ๊ฐ๋ค.
- ์ฃผ์ด์ง ํญ๊ณต๊ถ์ ๋ชจ๋ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- ๋ชจ๋ ๋์๋ฅผ ๋ฐฉ๋ฌธํ ์ ์๋ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์์ต๋๋ค.
- ๋ชจ๋ ๋์๋ฅผ ๋ฐฉ๋ฌธํ ์ ์๋ ๊ฒฝ์ฐ๋ ์ฃผ์ด์ง์ง ์๋๋ค?
- ๋ชจ๋ ๋์... ๋ฐฉ๋ฌธ...
- ๋ฐฉ๋ฌธ๋ง ํ๋ค๋๊ฑด๊ฐ?
- ์ด๋ฐ ์ผ์ด์ค๋ค.
- ICN -> A -> B๋ก ๊ฐ๋ฉด ๋๋๋ ์ผ์ด์ค.
- ์ ๋ต์
- ICN -> A -> C -> D -> Z -> A -> B
- ๊ทธ๋์ผ ๋ชจ๋ ๊ฒฝ๋ก๋ก ์ฌํ์ ๊ฐ ์ ์๋ค.
- DFS๋ก ํ์ด์ผ ํ๋ค...
- ๋ด์ผ ๋ค์ ํธ๋ ๊ฑธ๋ก ใ ใ ใ
- ๋ฐฉ๋ฌธ๋ง ํ๋ค๋๊ฑด๊ฐ?
- ๋ชจ๋ ๋์... ๋ฐฉ๋ฌธ...
- ๋๋์๊ฐ๊ธฐ๋ฅผ ๊ตฌํํด์ผ ํ๋๋ฐ...
- ์คํ์ผ๋ก ํ๋ฉด? ์ ๊ด๋ฆฌํ๊ณ ๋ค๋ก ๋๋ฆฌ๋ฉด์ ์ง์๋ ๊ฑฐ ๋ณต๊ตฌ ์ํค๊ณ ๊ท์ฐฎ๋ค.
- ๊ทธ๋ฅ ๊ฐ๋ฆผ๊ธธ์ด ๋์ค๋ฉด ์ฌ๊ท ํธ์ถ ํด๋ฒ๋ ค? ๊ณตํญ ๊ฐฏ์๋ ์ต๋ 1๋ง๊ฐ... ๋๋ฆด๋ ค๋?
- ๊ทผ๋ฐ ์ง๋ฉด์ ์ด๊ฑฐ ๋๋ฌด ํ์ด์ฌ์ค๋ฝ์ง ์์ ์ฝ๋ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.
- ์ ๋ชฐ๋ผ... ์๊ณ ์ถ์ด.
- ๊ทผ๋ฐ ์ง๋ฉด์ ์ด๊ฑฐ ๋๋ฌด ํ์ด์ฌ์ค๋ฝ์ง ์์ ์ฝ๋ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.
728x90
๋ฐ์ํ
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค [3์ฐจ] ์์ถ (0) | 2023.02.26 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ์ผ๊ทผ์ง์ (0) | 2023.02.25 |
ํ๋ก๊ทธ๋๋จธ์ค ๋จ์ด ๋ณํ -๊น์ด/๋๋น ์ฐ์ ํ์(DFS/BFS) (0) | 2023.02.25 |
ํ๋ก๊ทธ๋๋จธ์ค ๋คํธ์ํฌ - ๊น์ด/๋๋น ์ฐ์ ํ์(DFS/BFS) (0) | 2023.02.24 |
ํ๋ก๊ทธ๋๋จธ์ค ํผ์์ ํ๋ ํฑํํ (2) | 2023.02.24 |