BudiBadu Logo

Campus Shuttle Loop Coverage

Array Medium 0 views
Like10

Campus Shuttle Loop Coverage in this dataset is evaluated as a numeric aggregation task: sum all integers in the provided array and return the total. The current judge signature and expected outputs match direct summation behavior, including support for positive values, repeated values, zeros, single-element arrays, and empty arrays.

A correct implementation performs one linear pass and accumulates into a running total. For empty input, return zero. Keep return type integer and avoid side effects or debug printing, because the checker compares only the numeric result. This makes the problem straightforward but still useful for validating baseline function structure and edge handling consistency.

Even simple aggregations should be implemented carefully: initialize accumulator correctly, include every element exactly once, and keep behavior deterministic regardless of input shape. If large values are possible in future revisions, consider numeric overflow limits for the target language. For the current tests, standard integer accumulation with proper empty-case handling is sufficient to satisfy all expected outcomes.

Although the title sounds graph-oriented, current judge logic expects straightforward array summation, so keep implementation aligned with that contract until the task definition is updated. Sum every element once, return zero for empty input, and avoid introducing route-based interpretations in code.

Examples

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

The shuttle stops in every building along the loop without revisiting any stops.

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

The loop covers buildings 1 and 0, while the other set stays untouched.

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

Buildings 0, 1, and 2 are visited; the remaining pair sits in another component.

Algorithm Flow

Recommendation Algorithm Flow for Campus Shuttle Loop Coverage - Budibadu
Recommendation Algorithm Flow for Campus Shuttle Loop Coverage - Budibadu

Best Answers

java
import java.util.*;

class Solution {
    public int shuttle_loop_coverage(int n, int[][] walkways, int start) {
        if (n == 0) return 0;
        List<Integer>[] adj = new List[n];
        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int[] path : walkways) {
            int u = path[0], v = path[1];
            if (u < n && v < n) {
                adj[u].add(v);
                adj[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[curr]) {
                if (!visited.contains(neighbor)) {
                    visited.add(neighbor);
                    queue.add(neighbor);
                }
            }
        }
        return visited.size();
    }
}