Code Logo

Metro Transfer Capacity Map

Published atDate not available
Easy 0 views
Like0

You can think of this as a small game with a very specific goal. In Metro Transfer Capacity Map, you are trying to work toward the right number by following one clear idea.

Here, you start with one piece of information and turn it into something cleaner or more useful. You might keep only certain items, change their shape, or fix how text looks. The important part is to follow the steps in the right order. If you do that carefully, the final result comes out just the way the problem expects.

For example, if the input is n = 6, lines = [[0,2],[2,3],[3,1],[1,5],[5,4]], start = 3, suspended_stations = [1,5], the answer is 3. The team can reach stations 3, 2, and 0 but cannot pass through the suspended stops. Another example is n = 7, lines = [[0,1],[1,2],[2,3],[3,4],[1,5],[5,6]], start = 0, suspended_stations = [4], which gives 6. Stations 0, 1, 2, 3, 5, and 6 receive the update while station 4 remains closed.

This is a friendly practice problem, but it still rewards careful reading. The key is doing the steps in the right order and not changing things you should keep.

Example Input & Output

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

The team can reach stations 3, 2, and 0 but cannot pass through the suspended stops.

Example 2
Input
n = 7, lines = [[0,1],[1,2],[2,3],[3,4],[1,5],[5,6]], start = 0, suspended_stations = [4]
Output
6
Explanation

Stations 0, 1, 2, 3, 5, and 6 receive the update while station 4 remains closed.

Example 3
Input
n = 4, lines = [[0,1],[1,2]], start = 2, suspended_stations = [0,1]
Output
1
Explanation

Only the origin station is open, so the message does not travel anywhere else.

Algorithm Flow

Recommendation Algorithm Flow for Metro Transfer Capacity Map
Recommendation Algorithm Flow for Metro Transfer Capacity Map

Best Answers

java
import java.util.*;

class Solution {
    public int metro_transfer_capacity_map(int n, int[][] lines, int start, int[] suspended_stations) {
        Set<Integer> suspended = new HashSet<>();
        for (int s : suspended_stations) suspended.add(s);
        List<List<Integer>> adj = new ArrayList<>();
        for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
        for (int[] line : lines) {
            int u = line[0], v = line[1];
            if (!suspended.contains(u) && !suspended.contains(v)) {
                adj.get(u).add(v);
                adj.get(v).add(u);
            }
        }
        Set<Integer> visited = new HashSet<>();
        visited.add(start);
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        while (!queue.isEmpty()) {
            int curr = queue.poll();
            for (int neighbor : adj.get(curr)) {
                if (!visited.contains(neighbor)) {
                    visited.add(neighbor);
                    queue.add(neighbor);
                }
            }
        }
        return visited.size();
    }
}