Frontend School

Fastest Route with Priority Lanes

DSA
medium

Fastest Route with Priority Lanes

Medium
Graph
Shortest Path
Dijkstra

Overview

Upgrad Ride-Share is developing an advanced routing system to provide the fastest possible travel times for its premium users. These users have access to a limited number of "priority lane" passes, which can significantly reduce travel time on congested road segments. Your task is to design an algorithm that calculates the absolute minimum travel time between two points in a city, strategically utilizing these priority passes. This problem is crucial for ensuring customer satisfaction and optimizing operational efficiency for our premium service tier.

What you need to do / Task

You are required to implement a function findFastestRouteWithPriorityLanes that determines the minimum time to travel from a start intersection to an end intersection. Your solution must satisfy the following:

  1. Model the city's intersections and roads as a graph, where intersections are nodes and roads are weighted edges (travel time).
  2. Account for k priority lane passes. Each pass can be used on one road segment to reduce its travel time to 0 for that specific traversal. A pass can only be used once.
  3. Return the minimum total travel time.

Input & output

Your function will receive the following parameters:

  • n: An integer representing the total number of intersections in the city, indexed from 0 to n-1.
  • roads: A 2D array of integers, where each inner array [u, v, time] represents an undirected road connecting intersection u and intersection v with a travel time of time.
  • start: An integer representing the starting intersection.
  • end: An integer representing the destination intersection.
  • k: An integer representing the number of priority lane passes available.

The function should return:

  • An integer representing the minimum total travel time from start to end. If the end intersection is unreachable, return -1.

Clarifications

  • Roads are undirected: if [u, v, time] exists, you can travel from u to v or v to u in time.
  • If start and end are the same, the travel time is 0.
  • You can only use a priority pass on a road segment once. If you traverse the same segment multiple times, only one traversal can benefit from a pass (if available).
  • Multiple roads might connect the same two intersections; consider them distinct edges.

Examples

1

Example 1

Input:

n = 3, roads = [[0,1,10],[1,2,5],[0,2,100]], start = 0, end = 2, k = 1

Output:

0

Note:

The fastest route is to use a priority pass on the direct road from 0 to 2, reducing its travel time from 100 to 0. Total time: 0.

2

Example 2

Input:

n = 4, roads = [[0,1,10],[0,2,2],[1,3,1],[2,3,100]], start = 0, end = 3, k = 1

Output:

1

Note:

The fastest route is to use one priority pass on the road from 0 to 1 (cost 10 -> 0), then travel from 1 to 3 (cost 1). Total time: 0 + 1 = 1.

3

Example 3

Input:

n = 4, roads = [[0,1,10],[1,2,5],[2,3,1]], start = 0, end = 3, k = 0

Output:

16

Note:

With no priority passes, the route is a standard shortest path: 0 -> 1 (10) -> 2 (5) -> 3 (1). Total time: 16.

4

Example 4

Input:

n = 3, roads = [[0,1,10]], start = 0, end = 2, k = 0

Output:

-1

Note:

Intersection 2 is unreachable from intersection 0, even with priority passes (as there are no roads connecting to 2 from 0 or 1).

Constraints

Constraints & assumptions

  • 1 <= n <= 10^4
  • 0 <= roads.length <= 10^5
  • roads[i].length == 3
  • 0 <= u, v < n, u != v
  • 1 <= time <= 1000
  • 0 <= start, end < n
  • 0 <= k <= 10 (Number of priority passes is limited to prevent state explosion)
  • The graph may be disconnected.

What interviewers evaluate

  • Algorithmic Correctness: Correct application of a shortest path algorithm (e.g., Dijkstra's) adapted for the state space.
  • State Management: Effective modeling of the problem state to incorporate the k priority lane passes.
  • Efficiency: Optimal use of data structures (e.g., priority queue) to achieve the required time complexity.
  • Edge Case Handling: Proper handling of scenarios like unreachable destinations, start == end, or k=0.
  • Code Quality: Readability, modularity, and maintainability of the solution.

Loading editor...