Skyway Signal Cluster
This problem feels like a little puzzle you can solve one step at a time. In Skyway Signal Cluster, you are trying to work toward the right number by following one clear idea.
Find skyway signal cluster coverage A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.
For example, if the input is n = 4, connectors = [], start = 1, the answer is 1. With no connectors, the broadcast remains at the starting deck. Another example is n = 5, connectors = [[0,1],[1,2],[2,3],[3,4]], start = 2, which gives 5. Every deck is connected through the chain, so the broadcast covers all decks.
This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.
Example Input & Output
With no connectors, the broadcast remains at the starting deck.
Every deck is connected through the chain, so the broadcast covers all decks.
The cluster includes decks 2, 3, and 4; the other decks are unreachable.
Algorithm Flow
Solution Approach
This problem asks us to count the number of distinct signal clusters in a grid. A cluster is a group of connected 1 cells, where cells connect only through their four direct neighbors (up, down, left, right). Diagonal cells do not belong to the same cluster.
The classic way to solve this is a grid traversal. We scan every cell, and whenever we find an unvisited 1, we have found a new cluster, so we count it and flood-fill to mark its whole cluster as visited.
Here is the implementation using an iterative flood fill:
We keep a visited grid and scan every cell. When we hit an unvisited 1, we increment the cluster count and start a flood fill from that cell. The fill uses the four direction offsets and marks every connected 1 as visited, so later cells in the same cluster are skipped.
Let us trace grid = [[1,1,0],[1,1,0],[0,0,1]]. The four 1 cells in the top-left corner form one connected cluster, and the single 1 at the bottom-right forms another, giving 2. For [[1,1],[1,1]], all four cells are connected into one cluster, so the answer is 1.
Diagonal-only cells, like the pattern [[1,0],[0,1]], are separate because they do not share a side. In the 3x3 example [[1,0,1],[0,1,0],[1,0,1]], every 1 is isolated diagonally, so there are 5 separate clusters.
The time complexity is O(rows * cols) because each cell is visited once, and the space complexity is O(rows * cols) for the visited grid.
Best Answers
class Solution {
public int group_signal_patterns(int[][] grid) {
if (grid.length == 0) return 0;
int count = 0;
for (int r = 0; r < grid.length; r++)
for (int c = 0; c < grid[0].length; c++)
if (grid[r][c] == 1) { count++; dfs(grid, r, c); }
return count;
}
private void dfs(int[][] g, int r, int c) {
if (r < 0 || r >= g.length || c < 0 || c >= g[0].length || g[r][c] == 0) return;
g[r][c] = 0;
dfs(g, r-1, c); dfs(g, r+1, c); dfs(g, r, c-1); dfs(g, r, c+1);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
