Uber has launched a new circular shuttle service that operates on a fixed route, starting and ending at the same location while collecting passengers from various designated stops.
The service area consists of n pickup locations, numbered from 0 to n-1. These locations are interconnected by roads, each with a uniform travel cost of 1 unit. The road network is defined by n-1 edges, where each edge [ai, bi] represents a bidirectional road connecting locations ai and bi. This configuration forms a connected tree structure of pickup locations.
Passenger demand is represented by an array of size n, where passengers[i] equals 1 if there's a passenger waiting at location i, and 0 otherwise.
The shuttle service operates as follows: The shuttle may begin its route at any pickup location. Two operations are available at each stop:
Objective: Determine the minimum total cost required to collect all waiting passengers and return the shuttle to its starting location.
Important Constraints:
Goal: Find the optimal starting location and route strategy that minimizes the total operational cost.
Note: If you pass a road 2 times, the cost to traverse that road becomes 2.
Complete the function minimumShuttleCost in the editor.
minimumShuttleCost has the following parameters:
int[] passengers: A 1D array of integers wherepassengers[i] = 1indicates a passenger at pickup pointi, andpassengers[i] = 0indicates no passenger.int[][] edges: A 2D array of integers whereedges[i] = [ai, bi]indicates a bidirectional road connectingaiandbi.
Returns
int: Minimum cost incurred to start at a pickup point and return to the same starting point after collecting all the passengers from the pickup points.
passengers = [1, 0, 0, 0, 0, 1] edges = [[0,1],[1,2],[2,3],[3,4],[4,5]] return = 2
passengers = [0, 0, 0, 1, 1, 0, 0, 1] edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]] return = 2
1 ≤ n ≤ 3·10^4passengers.size()equalsn.0 ≤ passengers[i] ≤ 1edges.lengthequalsn-1.edges[i].lengthequals2.0 ≤ ai, bi < nai ≠ bi
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026
- Maximal Square AreaONSITE INTERVIEW · Seen May 2026
- First Unique IP Hitting the ServerPHONE SCREEN · Seen May 2026
- Jump Game with Prime-Step RuleOA · Seen May 2026
public int minimumShuttleCost(int[] passengers, int[][] edges) {
// write your code here
}