[JavaScript] 959. Regions Cut By Slashes
See this solution first\n- https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/solutions/3513630/javascript-2492-minimum-score-of-a-path-be
See this solution first\n- https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/solutions/3513630/javascript-2492-minimum-score-of-a-path-between-two-cities/\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\nSee a Union Find template here - https://www.hackerearth.com/practice/notes/disjoint-set-union-union-find/\n\nAssuming you have general Union Find template, you can find it on internet. Then have some custom functions created. In summary, code looks like below & Passes.\n\n```\nconst regionsBySlashes = function (g) {\n  const m = g.length\n  const n = g[0].length\n  const g2 = Array.from({ length: 3 * m }, () => Array(3 * n).fill(\'.\')) // temp grid\n\n  const a = []\n\n  for (let R = 0; R < m; R++) {\n    g[R] = g[R].split(\'\')\n    for (let C = 0; C < n; C++) {\n      for (let R2 = 0; R2 < 3; R2++) {\n        for (let C2 = 0; C2 < 3; C2++) {\n          a.push(3 * R + R2 + \',\' + (3 * C + C2))\n        }\n      }\n      if (g[R][C] === \'/\') {\n        g2[3 * R + 0][3 * C + 2] = g[R][C]\n        g2[3 * R + 1][3 * C + 1] = g[R][C]\n        g2[3 * R + 2][3 * C + 0] = g[R][C]\n      } else if (g[R][C] === \'\\\\\') {\n        g2[3 * R + 0][3 * C + 0] = g[R][C]\n        g2[3 * R + 1][3 * C + 1] = g[R][C]\n        g2[3 * R + 2][3 * C + 2] = g[R][C]\n      }\n    }\n  }\n\n  const uf = new UF({ n: a.length, A: a })\n\n  for (let R = 0; R < 3 * m; R++) {\n    for (let C = 0; C < 3 * n; C++) {\n      if (g2[R][C] === \'.\') {\n        if (g2[R][C + 1] === \'.\') {\n          uf.union(R + \',\' + C, R + \',\' + (C + 1))\n        }\n        if (g2[R + 1]?.[C] === \'.\') {\n          uf.union(R + \',\' + C, R + 1 + \',\' + C)\n        }\n      }\n    }\n  }\n\n  let cc = 0 // number of . groups\n  const rts = uf.roots()\n  for (const e of rts) {\n    const [R, C] = e.split(\',\')\n    if (g2[R][C] !== \'.\') {\n      continue\n    }\n    cc++\n  }\n  return cc\n}\n```\n
['JavaScript']