Unit 11 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
-1
if any camp cannot be reached from the starting camp.HAPPY CASE
Input: times = [(2, 1, 1), (2, 3, 1), (3, 4, 1)], n = 4, k = 2
Output: 2
Explanation: Starting from camp 2, the cure reaches camp 1 and camp 3 in 1 unit of time each, and reaches camp 4 via camp 3 in a total of 2 units of time.
EDGE CASE
Input: times = [(1, 2, 1)], n = 2, k = 2
Output: -1
Explanation: There is no direct or indirect communication line connecting camp 2 to camp 1, so camp 1 cannot be reached.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Shortest Path problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Build a graph representation of the network of camps, and use Dijkstra’s algorithm to determine the shortest travel time from the starting camp to all other camps. Return the maximum time needed to reach any camp.
1) Build a graph representation of the network where each node (camp) has a list of directed edges (communication lines).
2) Initialize a distance array with infinite values for all camps, except the starting camp, which has a distance of 0.
3) Use a min-heap (priority queue) to process camps in increasing order of travel time.
4) For each camp, explore its neighbors and update their distances if a shorter path is found.
5) Once all camps have been processed, return the maximum distance. If any camp is unreachable, return -1.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
import heapq
# Main function to find the minimum time to spread the cure to all camps
def spread_cure(n, times, k):
# Build the graph as a regular dictionary with empty lists for each node
graph = {i: [] for i in range(1, n + 1)}
for u, v, w in times:
graph[u].append((v, w))
# Distance array to keep track of the shortest time to each node
dist = {i: float('inf') for i in range(1, n + 1)}
dist[k] = 0 # The time to reach the starting camp is 0
# Min-heap to get the node with the smallest known distance
heap = [(0, k)] # (time, node) starting from node k
# Dijkstra's algorithm
while heap:
current_time, u = heapq.heappop(heap)
# If we already found a shorter way to u, skip
if current_time > dist[u]:
continue
# Explore neighbors
for v, w in graph.get(u, []):
time = current_time + w
# If a shorter time to v is found, update it and push to the heap
if time < dist[v]:
dist[v] = time
heapq.heappush(heap, (time, v))
# The time it takes for the cure to reach all camps is the maximum distance
max_time = max(dist.values())
# If there's a camp that is unreachable, return -1
return max_time if max_time != float('inf') else -1
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
-1
.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume E
is the number of edges (communication lines) and V
is the number of camps (nodes).
O(E log V)
because each edge is processed once and the heap operations take log V
time.O(E + V)
due to the graph representation and distance array.