LeetCode 0959. Regions Cut By Slashes Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
0959. Regions Cut By Slashes

Description

An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '\', or blank space ' '. These characters divide the square into contiguous regions.

Given the grid grid represented as a string array, return the number of regions.

Note that backslash characters are escaped, so a '\' is represented as '\\'.

 

Example 1:

Input: grid = [" /","/ "]
Output: 2

Example 2:

Input: grid = [" /","  "]
Output: 1

Example 3:

Input: grid = ["/\\","\\/"]
Output: 5
Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.

 

Constraints:

  • n == grid.length == grid[i].length
  • 1 <= n <= 30
  • grid[i][j] is either '/', '\', or ' '.

Solutions

Solution 1: Union-Find

PythonJavaC++GoTypeScriptJavaScript
class Solution: def regionsBySlashes(self, grid: List[str]) -> int: def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] def union(a, b): pa, pb = find(a), find(b) if pa != pb: p[pa] = pb nonlocal size size -= 1 n = len(grid) size = n * n * 4 p = list(range(size)) for i, row in enumerate(grid): for j, v in enumerate(row): k = i * n + j if i < n - 1: union(4 * k + 2, (k + n) * 4) if j < n - 1: union(4 * k + 1, (k + 1) * 4 + 3) if v == '/': union(4 * k, 4 * k + 3) union(4 * k + 1, 4 * k + 2) elif v == '\\': union(4 * k, 4 * k + 1) union(4 * k + 2, 4 * k + 3) else: union(4 * k, 4 * k + 1) union(4 * k + 1, 4 * k + 2) union(4 * k + 2, 4 * k + 3) return size(code-box)

Solution 2: DFS

TypeScriptJavaScript
function regionsBySlashes(grid: string[]): number { const createGraph = () => { const n = grid.length; const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0)); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { const [y, x] = [i * 2, j * 2]; switch (grid[i][j]) { case '/': g[y][x] = g[y + 1][x + 1] = 0; g[y][x + 1] = g[y + 1][x] = 1; break; case '\\': g[y][x] = g[y + 1][x + 1] = 2; g[y][x + 1] = g[y + 1][x] = 0; break; default: g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0; break; } } } return g; }; const isValid = (x: number) => 0 <= x && x < n; const dfs = (i: number, j: number) => { if (!isValid(i) || !isValid(j) || g[i][j]) return; g[i][j] = -1; const dirs = [-1, 0, 1, 0, -1]; const neighbours: number[] = []; for (let d = 0; d < 4; d++) { const [y, x] = [i + dirs[d], j + dirs[d + 1]]; if (isValid(y) && isValid(x)) { dfs(y, x); neighbours.push(g[y][x]); } else { neighbours.push(-1); } } const [top, right, bottom, left] = neighbours; if (top === 1 && right === 1) dfs(i - 1, j + 1); if (bottom === 1 && left === 1) dfs(i + 1, j - 1); if (top === 2 && left === 2) dfs(i - 1, j - 1); if (bottom === 2 && right === 2) dfs(i + 1, j + 1); }; const g = createGraph(); const n = g.length; let res = 0; for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { if (g[i][j] === 0) { dfs(i, j); res++; } } } return res; }(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !