๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Programming/Algorithm

(8)
[Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฌด์ธ๋„ ์—ฌํ–‰ ํŒŒ์ด์ฌ ํ’€์ด import sys sys.setrecursionlimit(10000) def dfs(x, y, maps, visit): val = 0 if x = len(maps[0]): return val if y = len(maps): return val if visit[y][x] is True: return val if maps[y][x] == "X": return val visit[y][x] = True return int(maps[y][x]) + dfs(x - 1 , y, maps, visit) + dfs(x + 1 , y, maps, visit) + dfs(x, y - 1, maps, visit) + dfs(x, y + 1, maps, visit) def solution(..
[Algorithm] Union - Find (Python ๊ตฌํ˜„) ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ํŒŒ์ด์ฌ ๊ตฌํ˜„ํ•˜๊ธฐ / union - find by Phython ์œ ๋‹ˆ์˜จ / ํŒŒ์ธ๋“œ๋Š” ํ˜„์žฌ ๋…ธ๋“œ๋“ค์ด ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ณ‘ํ•ฉ์ด ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. find ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ par = [ -1 for i in range(N + 1) ] # ๋ฃจํŠธ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด def find(x): # ๋ฃจํŠธ๋ฅผ ์ฐพ์•„์ฃผ๋Š” ํ•จ์ˆ˜ if par[x] ์•„์ง ์—ฐ๊ฒฐ๋œ ์ ์ด ์—†์Œ par[x] = find(par[x]) # ์ž์‹ ์˜ ๋ถ€๋ชจ๋ฅผ ๋ถ€๋ชจ->๋ถ€๋ชจ ๋ฅผ ์žฌ๊ท€๋กœ ๋ฃจํŠธ ๋ถ€๋ชจ๋ฅผ ์ฐพ์Œ return par[x] # ๋ฃจํŠธ ๋ถ€๋ชจ๋ฅผ ๋ฆฌํ„ด par ์ด๋ผ๋Š” ๋ฐฐ์—ด์—๋Š” ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ ๋‹ด์•„ ๋†“์Šต๋‹ˆ๋‹ค. par ๋ฐฐ์—ด์˜ index๋Š” ๊ฐ..
[Algorithm] ์žฌ๊ท€๋ฅผ ํ†ตํ•œ ์ˆœ์—ด(permutation) ๊ตฌํ˜„(+ python ๋‚ด์žฅ ํ•จ์ˆ˜ ์ด์šฉ) ์ˆœ์—ด์ด๋ž€, N๊ฐœ์˜ ์›์†Œ์—์„œ ์›ํ•˜๋Š” ๊ฐœ์ˆ˜๋ฅผ ๋ฝ‘์„ ๋•Œ, ์ˆœ์„œ๊นŒ์ง€ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ) [1, 2, 3] ๋ฐฐ์—ด์—์„œ 2๊ฐœ์˜ ์›์†Œ๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ => (1, 2), (1, 3), (2, 3), (2, 1), (2, 3), (3, 1), (3, 2) ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ (1, 2), (2, 1)์˜ ๊ฒฝ์šฐ๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฅธ ์žฌ๊ท€๋ฅผ ํ†ตํ•œ ์ฝ”๋“œ ๊ตฌํ˜„ test = [1, 2, 3] test_len = len(test) N = 2 # ๋ฝ‘์„ ์ˆœ์—ด์˜ ๊ฐœ์ˆ˜ visit = [0] * test_len # ํ•ด๋‹น index์˜ ๊ฐ’์„ ์‚ฌ์šฉํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€ arr = [0] * N # ํ˜„์žฌ ์ˆœ์—ด์„ ๋‹ด์„ ๋ฐฐ์—ด arr_list = [] # ๋ชจ๋“  ์ˆœ์—ด์„ ๋‹ด์„ ๋ฐฐ์—ด def permutaion(level): if level >=..
[Algorithm] ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ(Floyd Warshall) ์•Œ๊ณ ๋ฆฌ์ฆ˜(ft.ํŒŒ์ด์ฌ) ๐Ÿšฉ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŒŒ์ด์ฌ ๊ตฌํ˜„ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฐ™์ด ์ตœ๋‹จ๊ฑฐ๋ฆฌ ํ˜น์€ ์ตœ์†Œ๋น„์šฉ์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๋“ค๊นŒ์ง€์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ(ํ˜น์€ ์ตœ์†Œ๋น„์šฉ)์„ ๊ตฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— "์–ด๋”˜๊ฐ€๋ฅผ ๊ฒฝ์œ ํ•œ๋‹ค."๋ผ๋Š” ์กฐ๊ฑด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•˜๊ธฐ ์ข‹์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ถœ๋ฐœ์ (s), ๋„์ฐฉ์ง€์ (e), ๊ฒฝ์œ ์ง€(p)์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๊ณ ๋ คํ•ด์•ผํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N^3)์œผ๋กœ ํฐ ํŽธ ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋…ธ๋“œ(N)์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ณ ๋ คํ•ด ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด ์‚ฌ์šฉํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ์ง€ ๊ณ ๋ คํ•ด๋ด์•ผํ•ฉ๋‹ˆ๋‹ค. โœ๏ธ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ„ ๊ทธ๋ฆผ์˜ ๊ฒฝ์šฐ๋Š” ์ถœ๋ฐœ์ง€์ ์ด 1๋ฒˆ ๋…ธ๋“œ, ๋„์ฐฉ์ง€์ ์ด 4๋ฒˆ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ ์ž…๋‹ˆ๋‹ค. 1 -> 4๋กœ ๊ฐ€๋Š”๊ฒŒ ๊ฒฝ์œ ..
[Algoritm] ํ–‰๋ ฌ ํšŒ์ „ (Python ๊ตฌํ˜„) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ์นด์นด์˜ค 2020๋…„ ์‹ ์ž… ๊ฐœ๋ฐœ์ž ๋ธ”๋ผ์ธ๋“œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ 1์ฐจ ๋ฌธ์ œ 3๋ฒˆ์„ ํ’€์–ด ๋ณด์•˜๋‹ค. https://programmers.co.kr/learn/courses/30/lessons/60059 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ž๋ฌผ์‡ ์™€ ์—ด์‡  [[0, 0, 0], [1, 0, 0], [0, 1, 1]] [[1, 1, 1], [1, 1, 0], [1, 0, 1]] true programmers.co.kr ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ์•Œ์•„๋‚ด๋Š” ๊ฒƒ๋ณด๋‹ค ๊ตฌํ˜„๋ ฅ์ด ํ•„์š”ํ•œ ๋ฌธ์ œ์˜€๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ํ•ด๋‹น ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ์ œํ•œ ์‚ฌํ•ญ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํ™•์ธํ•˜๋Š” ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ, ํ–‰๋ ฌ์˜ ํšŒ์ „๊ณผ ๋ฒ”์œ„๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ–ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 2์ฐจ์› ํ–‰๋ ฌ์˜ ํšŒ์ „์„ ๊ตฌํ˜„ํ•ด์•ผ ..
[Algorithm] ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜ ๊ตฌํ•˜๊ธฐ GCD(Greatest Common Divisor) ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• ์‚ฌ์šฉ def gcd(a, b): while b: a, b = b, a % b return a a = b x q + r ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, GCD(a, b) = GCD(b, r)๋ฅผ ๋งŒ์กฑํ•œ๋‹ค. ๊ฐ„๋‹จ ์ฆ๋ช…: ์ˆ˜ํ•™์ ์œผ๋กœ ์ฆ๋ช…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. b = r x m + n์œผ๋กœ ํ‘œํ˜„ํ•  ๋•Œ, n์ด 0์ด๋ผ๋ฉด b = rm์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. ์ฆ‰, b์™€ r์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜ GCD(b, r) = m์ด ๋œ๋‹ค. a = bq + r = rmq + r = r(mq + 1) ์ด๋ฏ€๋กœ GCD(a, b) = r ์ด๋‹ค. => GCD(a, b) = GCD(b, r) = GCD(r, 0) = r LCM(Lo..
[Java] Kruskal's Algorithm ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(MST) ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ MST(Minimun Spanning Tree) - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ” ๊ตฌํ˜„ ๐Ÿง ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ MST(Minimun Spanning Tree)๋ž€? ์ตœ์†Œํ•œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์ •์ ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋‹ค. ์˜ˆ์‹œ) ๋„์‹œ๋“ค์€ ๋ชจ๋‘ ๋‹ค๋ฆฌ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์„ ๋•Œ, ๋ชจ๋“  ๋„์‹œ๋ฅผ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ๋‹ค๋ฆฌ๋ฅผ ์—ฐ๊ฒฐํ•˜๋˜ ์ตœ์†Œํ•œ์˜ ๋‹ค๋ฆฌ ๊ธธ์ด๋งŒ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (๋„์‹œ๊ฐ€ ๊ฐ€๋กœ๋“ฑ์œผ๋กœ ๋‹ค๋ฆฌ๊ฐ€ ์ „์„ ์œผ๋กœ ๋ฐ”๋€”์ˆ˜๋„ ์žˆ๋‹ค.) ๋„ค๋น„๊ฒŒ์ด์…˜์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ชจ๋“  ์ •์ (node ํ˜น์€ vortex)๊ฐ€ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ ์ค‘ ๊ฐ€์žฅ ์ ์€ ๊ฐ€์ค‘์น˜(์—ฌ๊ธฐ์„  ๊ฑฐ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ๋กœ ์ฃผ์˜์‚ฌํ•ญ ์—ฌ๊ธฐ์„œ ์—ฐ๊ฒฐ์ด๋ž€ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒƒ์„ ํฌํ•จํ•œ๋‹ค. ๊ฐ„์„  ํ˜น์€ ๋ธŒ๋žœ์น˜(๋…ธ๋“œ๋ฅผ ์ด์–ด์ฃผ๋Š” ์„ , ๊ฑฐ๋ฆฌ)๋Š” ๋ฐฉํ–ฅ์„ฑ์„ ๊ฐ–์ง€ ์•Š๋Š”๋‹ค.(์—ฐ๊ฒฐ๋งŒ ๋˜์–ด์žˆ..
[Java] Dijkstra Path ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„(ft. ์šฐ์„ ์ˆœ์œ„ ํ) ์‰ฝ์ง€๋งŒ ๋ณต์žกํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๐Ÿค” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์™œ ์“ธ๊นŒ? โ—๏ธ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ํŠน์ • ์ง€์ ์—์„œ ๋ชฉํ‘œ ์ง€์ ์œผ๋กœ ๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์„ ๋“ค์ด๋ฉฐ ๊ฐ€์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ) A์—์„œ D๋กœ ๊ฐ€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋น„์šฉ์€ ์ตœ์†Œ ๊ฐ’์€? A -> D = 5 A -> B -> D = 5 A -> C -> D = 3 ๋”ฐ๋ผ์„œ ์‹ค์ œ ์ตœ์†Œ ๋น„์šฉ ์ง€๋ถˆํ•˜๋Š” ๊ฒฝ๋กœ๋Š” A -> C -> D ์ด๋‹ค. ๐Ÿฅธ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋กœ์ง 1. ์‹œ์ž‘์ง€์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค๊ณผ์˜ ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค. Result List ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ A B C D E ๋น„์šฉ 0 3 1 5 ๋ฌดํ•œ ์ถœ๋ฐœ ์ง€์ ์ด A์ผ ๋•Œ, ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ ๊ฐ€๋Š” ๋น„์šฉ์€ ์—†์œผ๋‹ˆ 0 A์—์„œ E์™€ ๊ฐ™์ด ๋ฐ”๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒฝ๋กœ๋Š” ์—†์„ ๋•Œ, ๋น„์šฉ์„ ๋ฌดํ•œ(์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š” ์•„์ฃผ ํฐ ๊ฐ’)์œผ๋กœ ํ•œ๋‹ค..

728x90