DevLog ๐Ÿ“จ 24

[DevLog] [Alogorithm] ์ •๋ ฌ Sorting Problem

๐Ÿ‘† Sound of Sorting ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ถ„๋ฅ˜ Stable Sorting : ์ค‘๋ณต๋œ ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ๋œ ํ›„์—๋„ ์œ ์ง€๋จ์ด ๋ณด์žฅ๋œ๋‹ค. Unstable Sorting : ์ค‘๋ณต๋œ ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ์ •๋ ฌ๋œ ํ›„์— ์œ ์ง€๋จ์ด ๋ณด์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค. K1, .., K2, .. ์ˆœ์„œ์ธ ๋ฐฐ์—ด์—์„œ ์ •๋ ฌ ํ›„ K1, K2 ์ž„์ด ๋ณด์žฅ๋˜๋ฉด stable, K2, K1์ผ ์ˆ˜ ์žˆ์œผ๋ฉด unstable in-place: ๋‚ด๋ถ€ ์ •๋ ฌ, ์ •๋ ฌ์„ ์œ„ํ•ด ์™ธ๋ถ€ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. not in-place: ์™ธ๋ถ€ ์ •๋ ฌ, ์ •๋ ฌ์„ ์œ„ํ•ด ์™ธ๋ถ€ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค. ๐Ÿ”ฅ ํ•ต์‹ฌ์€ **๊ตํ™˜/์„ ํƒ/์‚ฝ์ž…** ์ด๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ด ์„ธ ๊ฐ€์ง€ ๊ฐœ๋…์„ ์‘์šฉํ•œ๋‹ค. 1. O(N^2) ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1. ๊ตํ™˜ ์ •๋ ฌ Exchange Sort for i in range(0, ..

[BaekJoon] 1007: ๋ฒกํ„ฐ ๋งค์นญ

BaekJoon 1007: ๋ฒกํ„ฐ ๋งค์นญ ํ’€์ด ๋ฐฉ๋ฒ• ์ง‘ํ•ฉ P๋Š” n๊ฐœ์˜ ์ ๋“ค์˜ ์ง‘ํ•ฉ์ด๊ณ , P์˜ ๋ฒกํ„ฐ ๋งค์นญ์€ P์˜ ๋ชจ๋“  ์ ์„ ํ•œ ๋ฒˆ์”ฉ ์‚ฌ์šฉํ•˜์—ฌ ๋งŒ๋“  ๋ฒกํ„ฐ์˜ ์ง‘ํ•ฉ์ด๋‹ค. ์ฆ‰, $P = {(x_1, y_1), (x_2, y_2), \dots , (x_n, y_n)}$ $VM = {(x_i - x_j,\space y_i - y-j)\space|\space 1 \le i, j \le n, i \ne j }$ ๋”ฐ๋ผ์„œ ๋ฒกํ„ฐ ๋งค์นญ์— ์†ํ•˜๋Š” ๋ชจ๋“  ๋ฒกํ„ฐ์˜ ํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. $v_1 = (x_2 - x_1, \space y_2 - y_1)$ $v_2 = (x_4 - x_3, \space y_4 - y_3)$ $ \dots $ $v_{n/2} = (x_n - x_{n-1}, \space y_n - y_{n-1})$ ..

[BaekJoon] 1005: ACM Craft

BaekJoon 1005: ACM Craft ํ’€์ด ๋ฐฉ๋ฒ• ์œ„์ƒ ์ •๋ ฌ topological sorting ๊ณผ DP๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์œ ํ–ฅ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค๊ณ , ์ง„์ž… ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด degree ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ์ •์ ์„ ๋ชจ๋‘ ๋‹ด์„ ํ๋„ ํ•˜๋‚˜ ์„ ์–ธํ•ด ์ค€๋‹ค. from collections import deque time = [0] + list(map(int, input().split())) # time to build result = time.copy() degree = [0] * (N + 1) graph = [[] for _ in range(N+1)] queue = deque([i+1 for i in range(N)]) ๋จผ์ €, ๊ฑด๋ฌผ์„ ์ง“๋Š” ์ˆœ์„œ X, Y ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ๋งˆ..

์œ„์ƒ ์ •๋ ฌ Topological Sorting

์œ„์ƒ ์ •๋ ฌ ์œ„์ƒ ์ •๋ ฌ์ด๋ž€? ์œ„์ƒ ์ •๋ ฌ Topological Sorting ์ด๋ž€ ๋ฐฉํ–ฅ์ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ๋“ค์„ ๋ณ€์˜ ๋ฐฉํ–ฅ์„ ๊ฑฐ์Šค๋ฅด์ง€ ์•Š๋„๋ก ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ ํ•œ ์ •์ ์— ๋„๋‹ฌํ•˜๊ธฐ ์ „ ๋จผ์ € ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ์ •์ ๋“ค์˜ ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋งŒ์•ฝ ํŠน์ • ์ˆ˜๊ฐ•๊ณผ๋ชฉ์— ์„ ์ˆ˜๊ณผ๋ชฉ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ์„ ์ˆ˜๊ณผ๋ชฉ๋ถ€ํ„ฐ ์ˆ˜๊ฐ•ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ํŠน์ • ๊ณผ๋ชฉ๋“ค์„ ์ˆ˜๊ฐ•ํ•ด์•ผ ํ•  ๋Œ€ ์œ„์ƒ ์ •๋ ฌ์„ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ์ˆ˜๊ฐ• ์ˆœ์„œ๋ฅผ ์ฐพ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์„ ํ›„ ๊ด€๊ณ„๊ฐ€ ์ •์˜๋œ ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ ์ƒ์—์„œ ์„ ํ›„ ๊ด€๊ณ„์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ์œ„์ƒ ์ •๋ ฌ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ •๋ ฌ์˜ ์ˆœ์„œ๋Š” ๋ฐฉํ–ฅ์ด ์žˆ๋Š” (์œ ํ–ฅ์˜) ๊ทธ๋ž˜ํ”„์˜ ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ข…๋ฅ˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์œ„์ƒ ์ •๋ ฌ์ด ์„ฑ๋ฆฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ๊ทธ๋ž˜ํ”„์˜ ์ˆœํ™˜์ด ์กด์žฌํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. ์ฆ‰, ๊ทธ๋ž˜ํ”„๊ฐ€..