Skip to content

Commit 1fc69b1

Browse files
committed
Add solution #1034
1 parent 29cde00 commit 1fc69b1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,105 LeetCode solutions in JavaScript
1+
# 1,106 LeetCode solutions in JavaScript
22

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

@@ -841,6 +841,7 @@
841841
1031|[Maximum Sum of Two Non-Overlapping Subarrays](./solutions/1031-maximum-sum-of-two-non-overlapping-subarrays.js)|Medium|
842842
1032|[Stream of Characters](./solutions/1032-stream-of-characters.js)|Hard|
843843
1033|[Moving Stones Until Consecutive](./solutions/1033-moving-stones-until-consecutive.js)|Medium|
844+
1034|[Coloring A Border](./solutions/1034-coloring-a-border.js)|Medium|
844845
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
845846
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
846847
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|

solutions/1034-coloring-a-border.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* 1034. Coloring A Border
3+
* https://leetcode.com/problems/coloring-a-border/
4+
* Difficulty: Medium
5+
*
6+
* You are given an m x n integer matrix grid, and three integers row, col, and color. Each value
7+
* in the grid represents the color of the grid square at that location.
8+
*
9+
* Two squares are called adjacent if they are next to each other in any of the 4 directions.
10+
*
11+
* Two squares belong to the same connected component if they have the same color and they are
12+
* adjacent.
13+
*
14+
* The border of a connected component is all the squares in the connected component that are
15+
* either adjacent to (at least) a square not in the component, or on the boundary of the grid
16+
* (the first or last row or column).
17+
*
18+
* You should color the border of the connected component that contains the square grid[row][col]
19+
* with color.
20+
*
21+
* Return the final grid.
22+
*/
23+
24+
/**
25+
* @param {number[][]} grid
26+
* @param {number} row
27+
* @param {number} col
28+
* @param {number} color
29+
* @return {number[][]}
30+
*/
31+
var colorBorder = function(grid, row, col, color) {
32+
const rows = grid.length;
33+
const cols = grid[0].length;
34+
const visited = new Set();
35+
const originalColor = grid[row][col];
36+
const borders = new Set();
37+
38+
function findBorders(r, c) {
39+
if (r < 0 || r >= rows || c < 0 || c >= cols
40+
|| visited.has(`${r},${c}`) || grid[r][c] !== originalColor) {
41+
return;
42+
}
43+
44+
visited.add(`${r},${c}`);
45+
46+
if (r === 0 || r === rows - 1 || c === 0 || c === cols - 1
47+
|| (r > 0 && grid[r - 1][c] !== originalColor)
48+
|| (r < rows - 1 && grid[r + 1][c] !== originalColor)
49+
|| (c > 0 && grid[r][c - 1] !== originalColor)
50+
|| (c < cols - 1 && grid[r][c + 1] !== originalColor)) {
51+
borders.add(`${r},${c}`);
52+
}
53+
54+
findBorders(r - 1, c);
55+
findBorders(r + 1, c);
56+
findBorders(r, c - 1);
57+
findBorders(r, c + 1);
58+
}
59+
60+
findBorders(row, col);
61+
borders.forEach(pos => {
62+
const [r, c] = pos.split(',').map(Number);
63+
grid[r][c] = color;
64+
});
65+
66+
return grid;
67+
};

0 commit comments

Comments
 (0)