Regions Cut By Slashes
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' '
.
Solution
With the cutted sections, we will join the sections depending on the slashes. For example, '/' means that north
joins west
and south
joins east
.
Note that we must also join the north
region of a square to the south
region of the square to the top of it. Similar for the left square's east
region and current square's west
region.
This is best approached by DSU, in the very end after joining all sections, we just have to find the number of components in the map regions
.
Implementation
def regionsBySlashes(grid: List[str]) -> int:
regions = {}
def find(x):
y = regions.get(x, x)
if y != x:
regions[x] = y = find(y)
return y
def union(x, y):
regions[find(x)] = find(y)
size = len(grid)
for i in range(size):
for j in range(size):
if i > 0: # connect square with top
union((i - 1, j, 'S'), (i, j, 'N'))
if j > 0: # connect square with left
union((i, j - 1, 'E'), (i, j, 'W'))
if grid[i][j] != "/": # ' ' or '\'
union((i, j, 'N'), (i, j, 'E')) # connect NE
union((i, j, 'S'), (i, j, 'W')) # connect SW
if grid[i][j] != "\\": # ' ' or '/'
union((i, j, 'N'), (i, j, 'W')) # connect NW
union((i, j, 'S'), (i, j, 'E')) # connect SE
return len(set(map(find, regions)))
Alternatively, we can find the number of connected components as in Number of Coneected Components, but bare in mind that we should only decrease the component count when we know that two elements aren't from the same set.
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorA heap is a ...?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!