๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์—ฌํ–‰๊ฒฝ๋กœ - DFS ๋ฌธ์ œ

๐ŸŽฎinspirer9 2023. 2. 25. 01:12
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
๋ฐ˜์‘ํ˜•