You need to sign in or sign up before continuing.×
5 kyu

GPS Navigation

493 of 495jakber

Description:

Your GPS is broken and you need to get somewhere!

Luckily you've got a map and your algorithm skills intact. Your map is a rather strange one too: all the intersections are labeled with distinct integers and the roads connecting them are labeled with the time they take to drive in minutes.

You are standing at the intersection labeled start and your destination is the intersection labeled finish.

You will be provided the total number of intersections and an array of roads, each with the properties from, to (both intersection labels; integers less than the number of intersections) and drivingTime. Roads may only be used to go from from to to. If there is no road going the other way it is a one-way street.

Complete the function navigate(numberOfIntersections, roads, start, finish) so that it returns an array of intersections on the fastest route from start to finish (including start and finish).

If there are several such routes, pick any. If there are no such routes, return null.

For example:

var roads = [
    {from: 0, to: 1, drivingTime: 5},
    {from: 0, to: 2, drivingTime: 10},
    {from: 1, to: 2, drivingTime: 10},
    {from: 1, to: 3, drivingTime: 2},
    {from: 2, to: 3, drivingTime: 2},
    {from: 2, to: 4, drivingTime: 5},
    {from: 3, to: 2, drivingTime: 2},
    {from: 3, to: 4, drivingTime: 10}
];
navigate(5, roads, 0, 4); 
// should return [0, 1, 3, 2, 4]. Fastest time is 5 + 2 + 2 + 5 = 14 minutes
Algorithms
Graph Theory

Similar Kata:

More By Author:

Check out these other kata created by jakber

Stats:

CreatedSep 26, 2013
PublishedSep 26, 2013
Warriors Trained2294
Total Skips714
Total Code Submissions15841
Total Times Completed495
JavaScript Completions493
Total Stars183
% of votes with a positive feedback rating92% of 107
Total "Very Satisfied" Votes93
Total "Somewhat Satisfied" Votes11
Total "Not Satisfied" Votes3
Ad
Contributors
  • jakber Avatar
  • jhoffner Avatar
  • Voile Avatar
Ad