Skip to content

Commit c8c7a5a

Browse files
committedMar 28, 2025
Add solution #959
1 parent 326d8e4 commit c8c7a5a

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,044 LeetCode solutions in JavaScript
1+
# 1,045 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -768,6 +768,7 @@
768768
956|[Tallest Billboard](./solutions/0956-tallest-billboard.js)|Hard|
769769
957|[Prison Cells After N Days](./solutions/0957-prison-cells-after-n-days.js)|Medium|
770770
958|[Check Completeness of a Binary Tree](./solutions/0958-check-completeness-of-a-binary-tree.js)|Medium|
771+
959|[Regions Cut By Slashes](./solutions/0959-regions-cut-by-slashes.js)|Medium|
771772
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
772773
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
773774
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 959. Regions Cut By Slashes
3+
* https://leetcode.com/problems/regions-cut-by-slashes/
4+
* Difficulty: Medium
5+
*
6+
* An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of
7+
* a '/', '\', or blank space ' '. These characters divide the square into contiguous
8+
* regions.
9+
*
10+
* Given the grid grid represented as a string array, return the number of regions.
11+
*
12+
* Note that backslash characters are escaped, so a '\' is represented as '\\'.
13+
*/
14+
15+
/**
16+
* @param {string[]} grid
17+
* @return {number}
18+
*/
19+
var regionsBySlashes = function(grid) {
20+
const size = grid.length * 3;
21+
const matrix = new Array(size).fill().map(() => new Array(size).fill(0));
22+
23+
for (let row = 0; row < grid.length; row++) {
24+
for (let col = 0; col < grid.length; col++) {
25+
const baseRow = row * 3;
26+
const baseCol = col * 3;
27+
if (grid[row][col] === '/') {
28+
matrix[baseRow][baseCol + 2] = 1;
29+
matrix[baseRow + 1][baseCol + 1] = 1;
30+
matrix[baseRow + 2][baseCol] = 1;
31+
} else if (grid[row][col] === '\\') {
32+
matrix[baseRow][baseCol] = 1;
33+
matrix[baseRow + 1][baseCol + 1] = 1;
34+
matrix[baseRow + 2][baseCol + 2] = 1;
35+
}
36+
}
37+
}
38+
39+
let result = 0;
40+
for (let row = 0; row < size; row++) {
41+
for (let col = 0; col < size; col++) {
42+
if (matrix[row][col] === 0) {
43+
helper(row, col);
44+
result++;
45+
}
46+
}
47+
}
48+
49+
return result;
50+
51+
function helper(x, y) {
52+
if (x < 0 || x >= size || y < 0 || y >= size || matrix[x][y] !== 0) return;
53+
matrix[x][y] = 1;
54+
helper(x + 1, y);
55+
helper(x - 1, y);
56+
helper(x, y + 1);
57+
helper(x, y - 1);
58+
}
59+
};

0 commit comments

Comments
 (0)
Please sign in to comment.