Understand what the interviewer is asking for by using test cases and questions about the problem.
[start_dest, end_dest]
is covered by at least one trip.trips
and two integers start_dest
and end_dest
.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a set of all destinations in the route, then remove destinations covered by the trips. If the set is empty, all destinations are covered.
1) Create a set `needed` of all destinations from `start_dest` to `end_dest`.
2) Iterate through the `trips` array:
- For each trip, remove the covered destinations from `needed`.
3) If `needed` is empty, return `True`; otherwise, return `False`.
⚠️ Common Mistakes
def is_route_covered(trips, start_dest, end_dest):
# Create a set of all destinations from start_dest to end_dest
needed = set(range(start_dest, end_dest + 1))
# Remove covered destinations from the set
for start, end in trips:
for dest in range(start, end + 1):
if dest in needed:
needed.remove(dest)
# If the set is empty, all destinations were covered
return len(needed) == 0