Count Isolated Binary Cells
Imagine you are analyzing sensor data from a grid-based monitoring system, where each cell represents a specific area and contains either a 0 or a 1. A 1 indicates an active signal, and a 0 indicates no signal. For certain anomaly detection tasks, it's crucial to identify "isolated" active signals – those that are not connected to any other active signals. This problem requires you to implement a function that counts such isolated active signals in a given binary grid.
You are given a 2D binary matrix (grid) representing sensor data. Your task is to implement a function countIsolatedCells that returns the total number of "isolated" cells in the grid.
An active cell (a cell containing 1) is considered "isolated" if all its immediate orthogonal neighbors (up, down, left, right) are 0s.
1 can be considered isolated. Cells containing 0 are never isolated.The function countIsolatedCells takes one argument:
grid: A 2D array of numbers (number[][]) representing the binary matrix. Each element will be either 0 or 1.The function should return:
[] or [[]]) should return 0.0s and 1s.(r, c) at the edge of the grid only considers its available neighbors. For example, grid[0][0] only has grid[0][1] (right) and grid[1][0] (down) as potential neighbors to check. If all such valid neighbors are 0s (or there are no valid neighbors), the cell is isolated.Examples
Example 1
Input:
grid = [[1,0,1],[0,0,0],[1,0,1]]
Output:
4
Note:
Cells at (0,0), (0,2), (2,0), and (2,2) are all '1's and have only '0's as immediate neighbors. For example, (0,0) has neighbors (0,1) and (1,0), both of which are '0'.
Example 2
Input:
grid = [[1,1,1],[1,0,1],[1,1,1]]
Output:
0
Note:
Every '1' cell has at least one '1' as an immediate neighbor, so none are isolated. For instance, (0,0) has (0,1) and (1,0) as '1' neighbors.
Example 3
Input:
grid = [[0,0,0],[0,1,0],[0,0,0]]
Output:
1
Note:
Only the cell at (1,1) is a '1' and all its immediate orthogonal neighbors (0,1), (2,1), (1,0), (1,2) are '0's.
Constraints
1 <= grid.length <= 100 (number of rows)1 <= grid[i].length <= 100 (number of columns for each row i)grid[i][j] is 0 or 1.grid[i].length is the same for all i).Loading editor...