LeetCode [JS] > 1496. Path Crossing

2025. 4. 8. 23:10ยท๐Ÿ”’Algorithm

๋ฌธ์ œ๋งํฌ

https://leetcode.com/problems/path-crossing/description/

๋ฌธ์ œํ’€์ด

์ด ๋ฌธ์ œ๋Š” Set ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค. Set ๊ฐ์ฒด๋Š” ์–ผํ• ๋ณด๋ฉด Arrayํ˜•ํƒœ๋ผ๊ณ  ์ƒ๊ฐ๋  ์ˆ˜ ๋„ ์žˆ๋Š”๋ฐ HashTable ํ˜•ํƒœ๋ฅผ ๋„๊ณ  ์žˆ๊ธฐ์— ์‹œ๊ฐ„๋ณต์žก๋„ ์ฐจ์›์—์„œ Array๋ณด๋‹ค ํ›จ์”ฌ ์ข‹๋‹ค. ( Set ๊ฐ์ฒด๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ key์™€ value๊ฐ€ ๋™์ผํ•œ ๊ตฌ์กฐ์ด๋ฉฐ add๋˜๋Š” ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋œ๋‹ค ๊ทธ๋ฆฌ๊ณ  iterable ํ•˜๊ธฐ์— ์ˆœํšŒํ•จ์ˆ˜๋ฅผ ์“ฐ๊ธฐ์—๋„ ์ข‹๋‹ค ). ํ•ด๋‹น ๋ฌธ์ œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n) ์ด๋‹ค.  

/**
 * @param {string} path
 * @return {boolean}
 */
var isPathCrossing = function (path) {
    let position = [0, 0];
    let set = new Set();

    set.add("0,0");

    for (const direction of path) {
        if (direction === "N") {
            ++position[0];
        } else if (direction === "S") {
            --position[0];
        } else if (direction === "E") {
            ++position[1];
        } else if (direction === "W") {
            --position[1];
        }

        let str = position[0] + "," + position[1];
        let size = set.size;
        set.add(str);

        if (size === set.size) {
            return true;
        }
    }

    return false;
};

'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

LeetCode[JS] > 350. Intersection of Two Arrays II  (0) 2025.04.16
LeetCode[JS] > 1913. Maximum Product Difference Between Two Pairs  (0) 2025.04.09
LeetCode [JS] > 1128. Number of Equivalent Domino Pairs  (0) 2025.04.07
LeetCode [JS] > 859. Buddy strings  (0) 2025.04.05
LeetCode [JS] > 2780. Minimum Index of a Valid Split  (0) 2025.04.05
'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • LeetCode[JS] > 350. Intersection of Two Arrays II
  • LeetCode[JS] > 1913. Maximum Product Difference Between Two Pairs
  • LeetCode [JS] > 1128. Number of Equivalent Domino Pairs
  • LeetCode [JS] > 859. Buddy strings
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐ŸงฉDev (263)
      • โญFE (34)
      • ๐Ÿ”’Algorithm (155)
      • โž•Etc. (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ๊ธ€์“ฐ๊ธฐ
    • ๊ด€๋ฆฌ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    leetcode
    DFS
    Lv2
    ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
    BFS
    ๊ณจ๋“œ5
    ์˜ค๋ธ”์™„
    ์‹ค๋ฒ„1
    nodejs
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    ์ฝ”ํ…Œ
    react
    ์‹ค๋ฒ„3
    Easy
    ํ”„๋ก ํŠธ์—”๋“œ
    ๋ฐฑ์ค€
    FE
    dp
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    javascript
    ๊ตฌํ˜„
    ์‹ค๋ฒ„2
    js
    Algorithm
    node.js
    ์‹ค๋ฒ„4
    ๊ทธ๋ฆฌ๋””
    ์ž์Šค
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
devWarrior
LeetCode [JS] > 1496. Path Crossing
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”