๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

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 ํ•จ์ˆ˜๋กœ ์ฐพ๋Š” ๊ณผ์ •์ด ์ถ”๊ฐ€์ ์ธ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ์Œ ... (์ด๊ฑธ์ฐพ๋Š”๋ฐ ๊ฝค๋‚˜ ํ•ด๋ฉจ๋‹ค.)

์–ด๋ ค์› ๋˜ ์ #

  1. ๋ฌธ์ œ ์ž์ฒด์˜ ๋‚œ์ด๋„๊ฐ€ ๊ฝค ์žˆ์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.

  2. ๊ทธ๋ฆผ์€ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ ธ ์žˆ์ง€๋งŒ ์ž…๋ ฅ ๊ฐ’์—์„œ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์–ด๋ ค์› ๋‹ค . ๊ฒฐ๊ตญ ๊ทธ๋ž˜ํ”„ ํ˜•ํƒœ์—์„œ dfs๋ฅผ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ–ˆ๋Š”๋ฐ, ์ฒ˜์Œ์— ํŠธ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋‹ˆ ๋ฌธ์ œํ•ด๊ฒฐ ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

  3. ํšจ์œจ์„ฑ ๋ฌธ์ œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•˜๋‹ค๊ฐ€ ์ œ์ถœํ•˜๊ณ  ๋‚˜์„œ ํšจ์œจ์„ฑ์ด ๋ชจ๋‘ ์‹คํŒจํ•˜์ž ํšจ์œจ์„ฑ์„๊ณ ๋ คํ•ด์•ผ ํ•จ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํšจ์œจ์„ฑ ๋ฌธ์ œ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ค์› ๋‹ค. (์–ด๋””์—์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š”์ง€ ์ฐพ๊ธฐ)

๋ฐฐ์šด ์ #

  • VSCode + Node + Jest ํ™˜๊ฒฝ์œผ๋กœ ๋””๋ฒ„๊น… ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ–ˆ๋‹ค.

    • F5 ํ‚ค๋งŒ ๋ˆ„๋ฅด๋ฉด VSCode์˜ ํ˜„์žฌ ์—ด๋ ค์žˆ๋Š” ํŒŒ์ผ์˜ ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ Jest๋กœ ๋””๋ฒ„๊น…๋ชจ๋“œ๋กœ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

    • .vscode/launch.json ์„ ๋‹ค๋ค˜๋‹ค.

    NodeJS, Jest, VSCode debugging

  • ํšจ์œจ์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด "JS๋ผ์„œ ๊ทธ๋Ÿฐ๊ฐ€" ํ•˜๋Š” ์ƒ๊ฐ๋ถ€ํ„ฐ ๋“ค์—ˆ๋Š”๋ฐ, ๊ฒฐ๊ตญ ๋ฌธ์ œ๋Š” ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ์— ์žˆ์—ˆ๋‹ค. ํšจ์œจ์„ฑ ๋ฌธ์ œ๋ฅผ ๊ณ ๋ คํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ#

2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ for Tech developers ๋ฌธ์ œํ•ด์„ค

[ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋™๊ตด ํƒํ—˜ ] ํ•ด์„ค ๋ฐ ์ฝ”๋“œ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋™๊ตด ํƒํ—˜