Logo
Hyunsu Blog
algorithm-graph

๐Ÿ“†Published :Jan 20, 2024 โ€ข

๐Ÿ“†Updated :Jan 20, 2024 โ€ข

โ˜•๏ธ3min

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ํ’€๊ธฐ

์ €๋ฒˆ ์ฃผ์— ์ตํžŒ ๊ฐœ๋…์„ ๋ฐ”ํƒ•์œผ๋กœ ์ด๋ฒˆ์ฃผ์—” ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค.

๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฌธ์ œ ๋ฐ”๋กœ ๋ณด๋Ÿฌ ๊ฐ€๊ธฐ (๋ฌธ์ œ ์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค )

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์ธ maps[n-1][n-1] ์— ๋„์ฐฉ ํ•˜๊ธฐ ๊นŒ์ง€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฒฝ๋กœ์ค‘์—๋” ๋” ๋น ๋ฅธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ๊ฒฝ๋กœ์— ํ•ด๋‹นํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜์ธ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์ด ๋ฌธ์ œ์˜ ๋ชฉํ‘œ์ด๋‹ค.

BFS๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ์— ๋„์ฐฉ์— ๋Œ€ํ•œ ๋ณด์žฅ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด ์›๋ฆฌ๋กœ ์ธํ•ด maps[n-1][n-1] ์— ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋„์ฐฉํ•˜์—ฌ ์—ฐ์‚ฐ ํ•˜๊ฒŒ ๋˜๋ฉด ์ตœ์†Œ๊ฐ’์ด ๋œ๋‹ค.

์ถœ๋ฐœ ์ง€์ ์—์„œ ํŠน์ • ์ง€์  ๊นŒ์ง€์˜ ๊ฒฝ๋กœ์—์„œ ํ•ด๋‹นํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ํŠธ๋ž˜ํ‚น ํ•˜๋Š” ๋ฐฉ๋ฒ•์€, ํ˜„์žฌ ์ง€์ ์—์„œ ๋‹ค์Œ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ, ๋‹ค์Œ ๋ชฉํ‘œ ์ง€์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ํ์— ๋„ฃ์„ ์ „ ๋ฐฉ๋ฌธ์„ ํŠธ๋ž˜ํ‚น ํ•˜๋Š” ๊ณต๊ฐ„์— ์—ฌํƒœ๊นŒ์ง€ ์™”๋˜ ๊ฒฝ๋กœ ๋‹ค์Œ ๋ฐฉ๋ฌธ ์ง€์  = ํ˜„์žฌ ์ง€์  ์นธ ๊ฐœ์ˆ˜ +1 ์„ ํ†ตํ•ด ์—…๋ฐ์ดํŠธ๋ฅผ ํ•ด์ค€๋‹ค.

/** * ์ตœ๋‹จ ๊ฑฐ๋ฆฌ : BFS ์‚ฌ์šฉ * 1. 4๋ฐฉํ–ฅ ํƒ์ƒ‰ * 2. visited ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ฐฉ๋ฌธ ์ง€์  ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค. (์ด์ „ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ + 1) * 3. queue์—๋Š” ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋ฅผ ๋„ฃ๋Š”๋‹ค. * 4. ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๊ฐ€ ์—†์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. */ function solution(maps) { let n = maps[0].length let m = maps.length let x = 0, y = 0 let visited = Array.from({ length: maps.length }, () => Array.from({ length: maps[0].length }, () => -1), ) //๋ถ๋™๋‚จ์„œ const dx = [0, 1, 0, -1] const dy = [-1, 0, 1, 0] //๋ถ ๋™ ๋‚จ ์„œ let queue = [[y, x]] visited[y][x] = 1 while (queue.length) { const [y, x] = queue.shift() for (let i = 0; i < 4; i++) { let ny = dy[i] + y let nx = dx[i] + x if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue // ๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๋ฉด continue; if (maps[ny][nx] === 0) continue // ๋ฒฝ์ด๋ฉด continue if (visited[ny][nx] !== -1) continue //์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฉด continue queue.push([ny, nx]) //๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ๋ฅผ queue์— ๋„ฃ๋Š”๋‹ค. visited[ny][nx] = visited[y][x] + 1 //๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ์‹œ ์ด์ „ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ +1 } } return visited[m - 1][n - 1] }

๋„คํŠธ์›Œํฌ

๋ฌธ์ œ ๋ฐ”๋กœ ๋ณด๋Ÿฌ ๊ฐ€๊ธฐ (๋ฌธ์ œ ์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค )

๋ฌธ์ œ์—์„œ ์ž๋ฃŒ๊ตฌ์กฐ ํŠน์„ฑ์— ๋Œ€ํ•œ ํžŒํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๋ฌธ์ œ : ๋„คํŠธ์›Œํฌ๋ž€ ์ปดํ“จํ„ฐ ์ƒํ˜ธ ๊ฐ„์— ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ๋œ ํ˜•ํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ B๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๊ณ , ์ปดํ“จํ„ฐ B์™€ ์ปดํ“จํ„ฐ C๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ C๋„ ๊ฐ„์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํ“จํ„ฐ A, B, C๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๋„คํŠธ์›Œํฌ ์ƒ์— ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ n, ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด computers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋„คํŠธ์›Œํฌ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์—ฌ๊ธฐ์„œ A์™€ C๊ฐ€ ๊ฐ„์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ๋Š” A-B-C๋กœ ์—ฐ๊ฒฐ์ด ๋œ๋‹ค. ์ด๋Š” ๊ทธ๋ž˜ํ”„์—์„œ ํŠธ๋ฆฌ๊ฐ€ ๊ฐ€์ง€๋Š” acyclic ํŠน์„ฑ์ด๋‹ค.

๋„คํŠธ์›Œํฌ์˜ ๊ฐœ์ˆ˜๋Š” ๊ณง ์ปดํ“จํ„ฐ๊ฐ€ ์—ฐ๊ฒฐ๋œ ์ง‘ํ•ฉ์˜ ๊ฐœ๋…์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋Š” ๊ณง ์ง‘ํ•ฉ์ด ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธ ํ•œ๋‹ค.

์ด๋Ÿฐ ํŠน์„ฑ์œผ๋กœ ๋ฏธ๋ฃจ์–ด ๋ณด์•„ BFS๋ณด๋‹ค๋Š” DFS๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ปดํ“จํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ  ๋Š๊ธฐ๋ฉด ๋‹ค๋ฅธ ์ง€์ ์—์„œ ๋‹ค์‹œ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค.

ํƒ์ƒ‰์„ ์œ„ํ•ด ์ฃผ์–ด์ง„ ์ธ์ ‘ ํ–‰๋ ฌ์—์„œ ์–ด๋–ป๊ฒŒ ํƒ์ƒ‰์„ ํ•  ๊ฒƒ์ธ๊ฐ€๋„ ์ค‘์š”ํ•˜๋‹ค. computers[i][j] = 1์ด๋ฉด ์—ฐ๊ฒฐ ๋˜์—ˆ๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ, index ๊ฐœ๋…์œผ๋กœ 0๋ฒˆ ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ์ด 3๊ฐœ์˜ ์ปดํ“จํ„ฐ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ ๋œ๋‹ค.

;[ [1, 1, 0], //i=0 , 0๋ฒˆ ์ปดํ“จํ„ฐ์˜ ์ธ์ ‘ ์ปดํ“จํ„ฐ๋“ค [1, 1, 0], //i=1 , 1๋ฒˆ ์ปดํ“จํ„ฐ์˜ ์ธ์ ‘ ์ปดํ“จํ„ฐ๋“ค [0, 0, 1], // i=2 , 2๋ฒˆ ์ปดํ“จํ„ฐ์˜ ์ธ์ ‘ ์ปดํ“จํ„ฐ๋“ค ]

๋Š๊ธด ์ง€์  ์—์„œ ๋‹ค์‹œ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ visited ๊ณต๊ฐ„์„ ํ†ตํ•ด ์ด๋ฏธ ํƒ์ƒ‰ํ–ˆ๋˜ ์ปดํ“จํ„ฐ๋Š” ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

if (!visited[i]) { dfs(i) cnt += 1 }

ํ’€์ด

function solution(n, computers) { const visited = Array.from({ length: n }, () => 0) let cnt = 0 // ๋ชจ๋“  ์ปดํ“จํ„ฐ ์ง€์ ์„ ๋Œ์•„์•ผ ํ•จ. ๋Š๊ธด ๋„คํŠธ์›Œํฌ์—์„œ ์ƒˆ๋กœ์šด ๋„คํŠธ์›Œํฌ๋ฅผ ํ˜•์„ฑํ•˜๊ธฐ ์œ„ํ•ด for (let i = 0; i < n; i++) { if (!visited[i]) { dfs(i) cnt += 1 } } return cnt function dfs(v) { visited[v] = 1 ;(computers[v] || []).forEach((value, idx) => !!value && !visited[idx] ? dfs(idx) : false, ) } }

๋ฐฐ๋‹ฌ

๋ฌธ์ œ ๋ฐ”๋กœ ๋ณด๋Ÿฌ ๊ฐ€๊ธฐ (๋ฌธ์ œ ์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค )

๋ฌธ์ œ ์ง€๋ฌธ ์ผ๋ถ€ N๊ฐœ์˜ ๋งˆ์„๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‚˜๋ผ์˜ ๊ฐ ๋งˆ์„์—๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋ถ€์—ฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ†ตํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๋กœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๋ฐ, ์„œ๋กœ ๋‹ค๋ฅธ ๋งˆ์„ ๊ฐ„์— ์ด๋™ํ•  ๋•Œ๋Š” ์ด ๋„๋กœ๋ฅผ ์ง€๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋„๋กœ๋ฅผ ์ง€๋‚  ๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ๋„๋กœ๋ณ„๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ˜„์žฌ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์—์„œ ๊ฐ ๋งˆ์„๋กœ ์Œ์‹ ๋ฐฐ๋‹ฌ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„๋กœ๋ถ€ํ„ฐ ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, N๊ฐœ์˜ ๋งˆ์„ ์ค‘์—์„œ K ์‹œ๊ฐ„ ์ดํ•˜๋กœ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ๋งˆ์„์—์„œ๋งŒ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์„์˜ ๊ฐœ์ˆ˜ N, ๊ฐ ๋งˆ์„์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ์ •๋ณด road, ์Œ์‹ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„ K๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ 1๋ฒˆ ๋งˆ์„ ๋ถ€ํ„ฐ K ์‹œ๊ฐ„ ์ดํ•˜๋กœ ๋ฐฐ๋‹ฌ ๊ฐ€๋Šฅํ•œ ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.

๋งˆ์„์ด ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ†ตํ–‰ ํ•  ์ˆ˜ ์žˆ๋Š” ์  ์„ ํ†ตํ•ด ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ž„์„ ์•Œ์ˆ˜ ์žˆ๋‹ค. ์—ฐ๊ฒฐ๋œ ๋งˆ์„์„ ์ด๋™ํ•˜๋ฉด์„œ ๋ˆ„์ ๋œ ์‹œ๊ฐ„์„ ํŠธ๋ž˜ํ‚น ๋ฐ ํŠน์ • ์‹œ๊ฐ„ ์ดํ•˜์ธ์ง€ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„  ๋‹ค์ต์ŠคํŠธ๋ผ์™€ BFS๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

๋‚ด๊ฐ€ ์ ‘๊ทผ ํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์ต์ŠคํŠธ๋ผ์˜ ๊ฐœ๋…์ธ ์—์„œ ํž™ ์ž๋ฃŒ๊ตฌ์กฐ์ธ heapify ๋กœ ํšจ์œจํ™”๋ฅผ ํ•˜์ง€ ์•Š๊ณ , ํ๋ฅผ ํ†ตํ•ด ๋จผ์ € road ๋‹ค์Œ์— ๋ฐฉ๋ฌธํ•  ๋งˆ์„๊ณผ ๋ˆ„์ ์‹œ๊ฐ„์„ ๊ฐ™์ด ์ €์žฅํ•œ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๊ณ„์†๋˜๋Š” ์‹คํŒจ๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์Šคํ„ฐ๋”” ์žฅ ๋‹˜์„ ํ†ตํ•ด ๋‚˜์˜ ์•„์ด๋””์–ด์™€ ๊ตฌํ˜„์— ๋ชจ์ˆœ์„ ๊นจ๋‹ซ๊ฒŒ ๋˜์—ˆ๋‹ค.

์ค‘์š”ํ•œ ์ ์€

  • ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๊ทธ ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๋Š” ๋ฐ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ๋„ ํ•จ๊ป˜ ๊ณ ๋ คํ•ด์•ผ ํ•จ.
  • ๊ตฌํ˜„ : visited ๋ฐฐ์—ด์„ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ๋Œ€์‹  ํ•ด๋‹น ๋…ธ๋“œ์— ๋„๋‹ฌํ•˜๋Š” ์ตœ์†Œ ๋น„์šฉ์œผ๋กœ ์‚ฌ์šฉ
function solution(N, road, K) { // adjacency list let adj_list = [] for (const [from, to, w] of road) { if (!adj_list[from]) adj_list[from] = [] adj_list[from].push([to, w]) if (!adj_list[to]) adj_list[to] = [] adj_list[to].push([from, w]) } let MAX_K = 500_001 let visited = Array.from({ length: N + 1 }, () => MAX_K) // bfs // queue let START = 1 let queue = [[START, 0]] let pathSet = new Set() // while while (queue.length) { let [v, w] = queue.shift() //์ธ๊ทผ ๋งˆ์„ adj_list[v]?.forEach((info, i) => { // queue์— ๋„ฃ์„ ๋•Œ [๋งˆ์„, ํ˜„์žฌ ๊ฐ€์ค‘์น˜ + ํ•ฉ์‚ฐ ๊ฐ€์ค‘์น˜ ] const [adj_v, adj_w] = info let future_w = w + adj_w if (future_w <= K && future_w < visited[adj_v]) { queue.push([adj_v, future_w]) visited[adj_v] = future_w } }) } return visited.filter((v) => v !== MAX_K).length }

์ „๋ ฅ๋ง์„ ๋‘˜๋กœ ๋‚˜๋ˆ„๊ธฐ

๋ฌธ์ œ ๋ฐ”๋กœ ๋ณด๋Ÿฌ ๊ฐ€๊ธฐ (๋ฌธ์ œ ์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค )

n๊ฐœ์˜ ์†ก์ „ํƒ‘์ด ์ „์„ ์„ ํ†ตํ•ด ํ•˜๋‚˜์˜ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์€ ์ด ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ํ˜„์žฌ์˜ ์ „๋ ฅ๋ง ๋„คํŠธ์›Œํฌ๋ฅผ 2๊ฐœ๋กœ ๋ถ„ํ• ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ–๊ฒŒ ๋˜๋Š” ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ๋งž์ถ”๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜ n, ๊ทธ๋ฆฌ๊ณ  ์ „์„  ์ •๋ณด wires๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ€๋Šฅํ•œ ๋น„์Šทํ•˜๋„๋ก ๋‘ ์ „๋ ฅ๋ง์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ, ๋‘ ์ „๋ ฅ๋ง์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†ก์ „ํƒ‘ ๊ฐœ์ˆ˜์˜ ์ฐจ์ด(์ ˆ๋Œ€๊ฐ’)๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

๋ฌธ์ œ์—์„œ ์–ด๋ ค์› ๋˜ ๋ถ€๋ถ„์€ ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผ ํ• ์ง€ ๊ฐ์ด ์žกํžˆ์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํŒŒ์•…ํ•˜๋ฉด ํžŒํŠธ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ์ œ์—์„œ ํžŒํŠธ๋กœ ํŠธ๋ฆฌ ํ˜•ํƒœ ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์‚ฌ์ดํด์ด ์—†๋‹ค๋Š” ์ ๊ณผ ํ˜„์žฌ ๋…ธ๋“œ ๋“ค์€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  wires๋ฅผ ํ†ตํ•ด ์–ด๋Š ์ „์„  ๋…ธ๋“œ๊ฐ€ ์ด์–ด ์กŒ๋Š”์ง€ ๋ช…์‹œ์ ์œผ๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค. wires์˜ ์š”์†Œ ์ฆ‰ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ๋‘๊ฐœ๋ฅผ ์ „์ฒด ๋…ธ๋“œ์—์„œ ์ œ๊ฑฐ ํ•ด์คŒ์œผ๋กœ์จ ์ „์„ ๋“ค ์ค‘ ํ•˜๋‚˜๋ฅผ ๋Š์–ด์„œ ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

function solution(n, wires) { let adj_list = Array.from({ length: n + 1 }, () => []) wires.forEach(([from, to]) => { adj_list[from].push(to) adj_list[to].push(from) }) let mins = [] // wires๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ„์„  ๋นผ๊ธฐ wires.forEach((wire) => { let visited = new Set() let deep_copy_adj = adj_list.map((row) => row.slice()) const [a, b] = wire deep_copy_adj[a].splice(deep_copy_adj[a].indexOf(b), 1) deep_copy_adj[b].splice(deep_copy_adj[b].indexOf(a), 1) let subtraction = 0 //์—ฐ๊ฒฐ๋˜์–ด์ง„ ๋…ธ๋“œ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ for (let i = 1; i <= n; i++) { if (visited.has(i)) continue // ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฉด continue let result1 = dfs(i, 0, deep_copy_adj, visited) // ์—ฐ๊ฒฐ ๋…ธ๋“œ ๊ฐœ์ˆ˜ subtraction = result1 - subtraction //์ฒซ ์ง‘ํ•ฉ์€ subtraction์œผ๋กœ ํ• ๋‹น, ๋‘๋ฒˆ ์งธ ์ง‘ํ•ฉ์€ ๋นผ๊ธฐ } mins.push(Math.abs(subtraction)) }) return Math.min(...mins) function dfs(i, level, adj_list, visited) { visited.add(i) level += 1 adj_list[i].forEach((v) => { if (visited.has(v)) return false level = dfs(v, level, adj_list, visited) }) return level } }

๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

ํ…Œ์ผ€์—์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ€์ ธ๊ฐ€๊ธฐ ์œ„ํ•ด ํŒŒ์ด์ฌ์œผ๋กœ ๋น ๋ฅด๊ฒŒ heapq๋ฅผ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.

์ œ์ถœ ์‹œ ์‹คํŒจ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค 24 ใ€‰ ์‹คํŒจ (0.11ms, 10.4MB) 92% ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ๋ฐฉํ–ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์„ ๋‹ค์‹œ ๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

def solution(board): dy=[-1,0,1,0] #๋ถ๋™๋‚จ์„œ dx=[0,1,0,-1] hq= [] heapq.heappush(hq, (0,None,0,0)) visited = [ [float("inf")]*len(board) for i in range(len(board)) ] while(hq): w, axis, y, x =heapq.heappop(hq) _axis = axis print(w, axis, y, x) for i in range(4): nx= x+dx[i] ny = y+dy[i] if(nx<0 or ny<0 or nx>=len(board) or ny>=len(board) or board[ny][nx] ==1): continue if axis == None: if( ny == y and nx!=x): _axis = "x" elif(nx == x and ny!=y): _axis = "y" next_axis = _axis if( ny == y and nx!=x): next_axis = "x" elif(nx == x and ny!=y): next_axis = "y" isCorner = _axis != next_axis corner_w = 500 if isCorner else 0 next_w = w+100+corner_w # visited ๊ฐ’๊ณผ ๋น„๊ต ํ•˜์—ฌ ์ž‘์œผ๋ฉด visited ๊ฐ’ ๊ฐฑ์‹  if next_w <= visited[ny][nx]: visited[ny][nx] = next_w heapq.heappush(hq, (next_w, next_axis, ny, nx)) #heapq์— ๋„ฃ๊ธฐ print(visited[-1][-1]) return visited[-1][-1]

Hi, I'm Hyunsu ๐Ÿ‘‹

Profile Image

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ฃผํ˜„์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ’๋ถ€ํ•˜๊ณ  ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ์ผ์— ๋ฟŒ๋“ฏํ•จ์„ ๋Š๋‚๋‹ˆ๋‹ค.

์˜ต์‹œ๋””์–ธ(Obsidian)์—์„œ ํ˜„์žฌ ๋ธ”๋กœ๊ทธ๋กœ ํ•˜๋‚˜์”ฉ ๊ธ€์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์— ์žˆ์–ด์š”. โ˜•๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป โ›ท

Github on ViewReach Me Out