Code Logo

Skyway Signal Cluster

Published at05 Jan 2026
Array Manipulation Easy 7 views
Like6

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

Example 1
Input
n = 4, connectors = [], start = 1
Output
1
Explanation

With no connectors, the broadcast remains at the starting deck.

Example 2
Input
n = 5, connectors = [[0,1],[1,2],[2,3],[3,4]], start = 2
Output
5
Explanation

Every deck is connected through the chain, so the broadcast covers all decks.

Example 3
Input
n = 6, connectors = [[0,1],[2,3],[3,4],[4,2]], start = 3
Output
3
Explanation

The cluster includes decks 2, 3, and 4; the other decks are unreachable.

Algorithm Flow

Recommendation Algorithm Flow for Skyway Signal Cluster

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:

function group_signal_patterns(grid) {
    if (!grid || grid.length === 0) return 0;
    const rows = grid.length, cols = grid[0].length;
    const visited = Array.from({ length: rows }, () => Array(cols).fill(false));
    let count = 0;
    const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]];
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            if (grid[r][c] === 1 && !visited[r][c]) {
                count++;
                const stack = [[r, c]];
                visited[r][c] = true;
                while (stack.length > 0) {
                    const [cr, cc] = stack.pop();
                    for (const [dr, dc] of dirs) {
                        const nr = cr + dr, nc = cc + dc;
                        if (nr >= 0 && nr < rows && nc >= 0 && nc < cols &&
                            grid[nr][nc] === 1 && !visited[nr][nc]) {
                            visited[nr][nc] = true;
                            stack.push([nr, nc]);
                        }
                    }
                }
            }
        }
    }
    return count;
}

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

java
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);
    }
}