-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# 959. Regions Cut By Slashes | ||
|
||
- Difficulty: Medium. | ||
- Related Topics: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Matrix. | ||
- Similar Questions: . | ||
|
||
## Problem | ||
|
||
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: | ||
|
||
![](https://assets.leetcode.com/uploads/2018/12/15/1.png) | ||
|
||
``` | ||
Input: grid = [" /","/ "] | ||
Output: 2 | ||
``` | ||
|
||
Example 2: | ||
|
||
![](https://assets.leetcode.com/uploads/2018/12/15/2.png) | ||
|
||
``` | ||
Input: grid = [" /"," "] | ||
Output: 1 | ||
``` | ||
|
||
Example 3: | ||
|
||
![](https://assets.leetcode.com/uploads/2018/12/15/4.png) | ||
|
||
``` | ||
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 `' '`. | ||
|
||
|
||
|
||
## Solution | ||
|
||
```javascript | ||
/** | ||
* @param {string[]} grid | ||
* @return {number} | ||
*/ | ||
var regionsBySlashes = function(grid) { | ||
var matrix = Array(3 * grid.length).fill(0).map(() => Array(3 * grid.length).fill(0)); | ||
for (var i = 0; i < grid.length; i++) { | ||
for (var j = 0; j < grid[i].length; j++) { | ||
if (grid[i][j] === '\\') { | ||
matrix[i * 3][j * 3] = 1; | ||
matrix[i * 3 + 1][j * 3 + 1] = 1; | ||
matrix[i * 3 + 2][j * 3 + 2] = 1; | ||
} else if (grid[i][j] === '/') { | ||
matrix[i * 3][j * 3 + 2] = 1; | ||
matrix[i * 3 + 1][j * 3 + 1] = 1; | ||
matrix[i * 3 + 2][j * 3] = 1; | ||
} | ||
} | ||
} | ||
var res = 0; | ||
for (var i = 0; i < matrix.length; i++) { | ||
for (var j = 0; j < matrix[i].length; j++) { | ||
if (matrix[i][j] === 0) { | ||
visitAndMark(matrix, i, j, ++res); | ||
} | ||
} | ||
} | ||
return res; | ||
}; | ||
|
||
var visitAndMark = function(matrix, i, j, num) { | ||
matrix[i][j] = num; | ||
matrix[i][j + 1] === 0 && visitAndMark(matrix, i, j + 1, num); | ||
matrix[i][j - 1] === 0 && visitAndMark(matrix, i, j - 1, num); | ||
matrix[i + 1]?.[j] === 0 && visitAndMark(matrix, i + 1, j, num); | ||
matrix[i - 1]?.[j] === 0 && visitAndMark(matrix, i - 1, j, num); | ||
}; | ||
``` | ||
**Explain:** | ||
nope. | ||
**Complexity:** | ||
* Time complexity : O(n ^ 2). | ||
* Space complexity : O(n ^ 2). |