2020 ์นด์นด์ค ์ธํด์ญ - ๋๊ตด ํํ
#
๋ฌธ์ ์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋๊ตด ํํ
#
๊ตฌํ1์ฐจ ๊ตฌํ (์ ํ์ฑ ํต๊ณผ, ํจ์จ์ฑ ์คํจ)
function solution(n, path, order) { const nodes = Array.from({length: n}, () => new Node([], 0, false)); const start = nodes[0]; const locks = []; for (const p of path) { nodes[p[0]].edges.push(p[1]); nodes[p[1]].edges.push(p[0]); } for (const o of order) { nodes[o[1]].prior = o[0]; } if (start.prior !== 0) { return false; } start.visited = true; start.edges.forEach((edgeNum) => visit(edgeNum, nodes, locks)); if (nodes.some((node) => node.visited === false)) { return false; } else { return true; }} function visit(nodeNum, nodes, locks) { const current = nodes[nodeNum]; const priorNode = nodes[current.prior]; if (current.visited === true) { return; } if (priorNode.visited === false) { locks.push(nodeNum); return; } current.visited = true; const openNum = locks.find((lockNum) => nodes[lockNum].prior === nodeNum); if (openNum) { visit(openNum, nodes, locks); } current.edges.forEach((edge) => visit(edge, nodes, locks));} class Node { constructor(edges, prior, visited) { this.edges = edges; this.prior = prior; this.visited = visited; }}
- ํจ์จ์ฑ ์คํจ์ ์์ธ์ด ์๊ฐ ์ด๊ณผ๋ ์์์ง๋ง ๋ฐํ์ ์๋ฌ๋ ์์์. โ ์ฌ๊ท ํธ์ถ๋ก ์ธํ stack overflow๋ก ํ๋จ
2์ฐจ ๊ตฌํ (์ ํ์ฑ ํต๊ณผ, ํจ์จ์ฑ 2/30 ์ฑ๊ณต)
function solution(n, path, order) { const nodes = Array.from({length: n}, () => new Node([], 0, false)); const start = nodes[0]; const stack = []; const locks = []; for (const p of path) { nodes[p[0]].edges.push(p[1]); nodes[p[1]].edges.push(p[0]); } for (const o of order) { nodes[o[1]].prior = o[0]; } if (start.prior !== 0) { return false; } start.visited = true; start.edges.forEach((edge) => stack.push(edge)); while (stack.length !== 0) { const nodeNum = stack.pop(); const availables = visit(nodeNum, nodes, locks); availables.forEach((availNum) => stack.push(availNum)); } if (nodes.some((node) => node.visited === false)) { return false; } else { return true; }} function visit(nodeNum, nodes, locks) { const current = nodes[nodeNum]; const priorNode = nodes[current.prior]; if (current.visited === true) { return []; } if (priorNode.visited === false) { locks.push(nodeNum); return []; } current.visited = true; const openNum = locks.find((lockNum) => nodes[lockNum].prior === nodeNum); if (openNum) { return [...current.edges, openNum]; } return [...current.edges];} class Node { constructor(edges, prior, visited) { this.edges = edges; this.prior = prior; this.visited = visited; }}
visit ํจ์๋ฅผ ์ฌ๊ทํธ์ถํ์ง ์๊ณ , ๋ฐฉ๋ฌธํ ๋ ธ๋ ๋ชฉ๋ก์ stack์ผ๋ก ๊ด๋ฆฌํ ๋ค visit ํจ์์์๋ ๋ฐฉ๋ฌธํ ๋ ธ๋ ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ๊ฒ๋ ์์ ํจ.
ํจ์จ์ฑ ํ ์คํธ์ 2๊ฐ ํ ์คํธ ์ผ์ด์ค์ ๋ํด์๋ง ํต๊ณผํ๋๋ฐ, ๋๋จธ์ง๋ ๋ชจ๋ ์๊ฐ์ด๊ณผ๋ก ์ธํ ์๋ฌ์์.
์ต์ข (์ ํ์ฑ, ํจ์จ์ฑ ๋ชจ๋ ํต๊ณผ)
function solution(n, path, order) { const nodes = Array.from({length: n}, () => new Node([], 0, false, 0)); const start = nodes[0]; const stack = []; for (const p of path) { nodes[p[0]].edges.push(p[1]); nodes[p[1]].edges.push(p[0]); } for (const o of order) { nodes[o[1]].prior = o[0]; } if (start.prior !== 0) { return false; } start.visited = true; start.edges.forEach((edge) => stack.push(edge)); while (stack.length !== 0) { const node = stack.pop(); const availables = visit(node, nodes); availables.forEach((availNum) => stack.push(availNum)); } if (nodes.some((node) => node.visited === false)) { return false; } else { return true; }} function visit(node, nodes) { const current = nodes[node]; const priorNode = nodes[current.prior]; if (current.visited === true) { return []; } if (priorNode.visited === false) { priorNode.next = node; return []; } current.visited = true; if (current.next) { return [...current.edges, current.next]; } return [...current.edges];} class Node { constructor(edges, prior, visited, next) { this.edges = edges; this.prior = prior; this.visited = visited; this.next = next; }}
- locks ๋ฐฐ์ด์ ๋ ธ๋ ๋ชฉ๋ก์ ๋ด์๋๊ณ , ์ด๋ฒ ๋ฐฉ๋ฌธ์ ํตํด ๋ฐฉ๋ฌธํ ์ ์๊ฒ ๋ ๋ ธ๋๋ฅผ๋ฐฐ์ด์์ find ํจ์๋ก ์ฐพ๋ ๊ณผ์ ์ด ์ถ๊ฐ์ ์ธ ์๊ฐ ๋ณต์ก๋๋ฅผ ๋ฐ์์์ผฐ์ ... (์ด๊ฑธ์ฐพ๋๋ฐ ๊ฝค๋ ํด๋ฉจ๋ค.)
#
์ด๋ ค์ ๋ ์ ๋ฌธ์ ์์ฒด์ ๋์ด๋๊ฐ ๊ฝค ์์๋ ๋ฌธ์ ์๋ค.
๊ทธ๋ฆผ์ ํธ๋ฆฌ ํํ๋ก ์ฃผ์ด์ ธ ์์ง๋ง ์ ๋ ฅ ๊ฐ์์ ํธ๋ฆฌ ํํ๋ก ๋ง๋๋ ๊ฒ์ ์ด๋ ค์ ๋ค . ๊ฒฐ๊ตญ ๊ทธ๋ํ ํํ์์ dfs๋ฅผ ์ฌ์ฉํด ํด๊ฒฐํ๋๋ฐ, ์ฒ์์ ํธ๋ฆฌ๋ผ๊ณ ์๊ฐํ๋ ๋ฌธ์ ํด๊ฒฐ ๋ฒ์ ๊ณ ๋ฏผํ๋๋ฐ ์ค๋๊ฑธ๋ ธ๋ค.
ํจ์จ์ฑ ๋ฌธ์ ๋ฅผ ๊ณ ๋ คํ์ง ์์๋ค๊ฐ ์ ์ถํ๊ณ ๋์ ํจ์จ์ฑ์ด ๋ชจ๋ ์คํจํ์ ํจ์จ์ฑ์๊ณ ๋ คํด์ผ ํจ์ ๊นจ๋ฌ์๋ค. ๊ทธ๋ฆฌ๊ณ ํจ์จ์ฑ ๋ฌธ์ ๋ฅผ ํ์ ํ๋ ๊ฒ์ด ์ด๋ ค์ ๋ค. (์ด๋์์ ์๊ฐ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ๋์ง ์ฐพ๊ธฐ)
#
๋ฐฐ์ด ์ VSCode + Node + Jest ํ๊ฒฝ์ผ๋ก ๋๋ฒ๊น ํ๊ฒฝ์ ๊ตฌ์ถํ๋ค.
F5
ํค๋ง ๋๋ฅด๋ฉด VSCode์ ํ์ฌ ์ด๋ ค์๋ ํ์ผ์ ์์ ๋๋ ํ ๋ฆฌ๋ฅผ Jest๋ก ๋๋ฒ๊น ๋ชจ๋๋ก ์คํ์ํฌ ์ ์๋ค..
vscode/launch.json
์ ๋ค๋ค๋ค.
ํจ์จ์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด "JS๋ผ์ ๊ทธ๋ฐ๊ฐ" ํ๋ ์๊ฐ๋ถํฐ ๋ค์๋๋ฐ, ๊ฒฐ๊ตญ ๋ฌธ์ ๋ ๋ด๊ฐ ์ง ์ฝ๋์ ์์๋ค. ํจ์จ์ฑ ๋ฌธ์ ๋ฅผ ๊ณ ๋ คํด์ผํ๋ค๋ ๊ฒ์ ๋ฐฐ์ ๋ค.
#
์ฐธ๊ณ ์๋ฃ2020 ์นด์นด์ค ์ธํด์ญ for Tech developers ๋ฌธ์ ํด์ค