Campus Shuttle Loop Coverage
This problem feels like a little puzzle you can solve one step at a time. In Campus Shuttle Loop Coverage, you are trying to work toward the right number by following one clear idea.
Here, you are mostly deciding whether a rule stays true while you look through the input. Sometimes that means checking if things are connected, balanced, or allowed. Sometimes it means noticing the first place where the rule breaks. The answer depends on being careful from beginning to end.
For example, if the input is n = 6, walkways = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, the answer is 6. The shuttle stops in every building along the loop without revisiting any stops. Another example is n = 4, walkways = [[0,1],[2,3]], start = 1, which gives 2. The loop covers buildings 1 and 0, while the other set stays untouched.
This problem needs a little more patience than a very easy one. The key is noticing the exact moment when the rule stays true or breaks.
Example Input & Output
The shuttle stops in every building along the loop without revisiting any stops.
The loop covers buildings 1 and 0, while the other set stays untouched.
Buildings 0, 1, and 2 are visited; the remaining pair sits in another component.
Algorithm Flow
Solution Approach
This problem asks us to compute the total coverage of a shuttle loop by summing the stop values in the input array. The answer is simply the sum of every element in the stops array.
Because the task is a straightforward sum, we can compute it with a loop that adds each value to a running total, or with a reduction helper. The order of the stops does not matter, only their total.
Here is the implementation:
We start with an accumulator of 0 and add every element of stops to it. The reduce function visits each value once and produces the final total.
For example, if stops = [5, 10, 3], the result is 5 + 10 + 3 = 18. If the array is empty, the reduce returns the initial value 0, which is the correct answer for no stops.
The time complexity is O(n) because we visit each element once, and the space complexity is O(1).
Best Answers
import java.util.*;
class Solution {
public int campus_shuttle_loop_coverage(int[] stops) {
int sum = 0;
for (int stop : stops) sum += stop;
return sum;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
