Skip to content

Commit 255d328

Browse files
committed
feat: add No.959
1 parent 66fc197 commit 255d328

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# 959. Regions Cut By Slashes
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table, Depth-First Search, Breadth-First Search, Union Find, Matrix.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
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.
10+
11+
Given the grid `grid` represented as a string array, return **the number of regions**.
12+
13+
Note that backslash characters are escaped, so a `'\'` is represented as `'\\'`.
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2018/12/15/1.png)
19+
20+
```
21+
Input: grid = [" /","/ "]
22+
Output: 2
23+
```
24+
25+
Example 2:
26+
27+
![](https://assets.leetcode.com/uploads/2018/12/15/2.png)
28+
29+
```
30+
Input: grid = [" /"," "]
31+
Output: 1
32+
```
33+
34+
Example 3:
35+
36+
![](https://assets.leetcode.com/uploads/2018/12/15/4.png)
37+
38+
```
39+
Input: grid = ["/\\","\\/"]
40+
Output: 5
41+
Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- `n == grid.length == grid[i].length`
50+
51+
- `1 <= n <= 30`
52+
53+
- `grid[i][j]` is either `'/'`, `'\'`, or `' '`.
54+
55+
56+
57+
## Solution
58+
59+
```javascript
60+
/**
61+
* @param {string[]} grid
62+
* @return {number}
63+
*/
64+
var regionsBySlashes = function(grid) {
65+
var matrix = Array(3 * grid.length).fill(0).map(() => Array(3 * grid.length).fill(0));
66+
for (var i = 0; i < grid.length; i++) {
67+
for (var j = 0; j < grid[i].length; j++) {
68+
if (grid[i][j] === '\\') {
69+
matrix[i * 3][j * 3] = 1;
70+
matrix[i * 3 + 1][j * 3 + 1] = 1;
71+
matrix[i * 3 + 2][j * 3 + 2] = 1;
72+
} else if (grid[i][j] === '/') {
73+
matrix[i * 3][j * 3 + 2] = 1;
74+
matrix[i * 3 + 1][j * 3 + 1] = 1;
75+
matrix[i * 3 + 2][j * 3] = 1;
76+
}
77+
}
78+
}
79+
var res = 0;
80+
for (var i = 0; i < matrix.length; i++) {
81+
for (var j = 0; j < matrix[i].length; j++) {
82+
if (matrix[i][j] === 0) {
83+
visitAndMark(matrix, i, j, ++res);
84+
}
85+
}
86+
}
87+
return res;
88+
};
89+
90+
var visitAndMark = function(matrix, i, j, num) {
91+
matrix[i][j] = num;
92+
matrix[i][j + 1] === 0 && visitAndMark(matrix, i, j + 1, num);
93+
matrix[i][j - 1] === 0 && visitAndMark(matrix, i, j - 1, num);
94+
matrix[i + 1]?.[j] === 0 && visitAndMark(matrix, i + 1, j, num);
95+
matrix[i - 1]?.[j] === 0 && visitAndMark(matrix, i - 1, j, num);
96+
};
97+
```
98+
99+
**Explain:**
100+
101+
nope.
102+
103+
**Complexity:**
104+
105+
* Time complexity : O(n ^ 2).
106+
* Space complexity : O(n ^ 2).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy