I have a problem I am working on as homework and I am having trouble figuring out how to solve it and if the approach I am taking can be optimized.
The problem is the following:
As an input I received this :
const inputFull = `08:20;Paris;Berlin
5
09:20;Paris;Amsterdam;03:20
08:30;Paris;Bruxelles;01:20
10:00;Bruxelles;Amsterdam;02:10
12:30;Amsterdam;Berlin;06:10
11:30;Bruxelles;Berlin;09:20`;
The first line is: start_date, start_city, finish_city
The second line is: Number of lines that will follow
The third line is: a list of all trajectories in this format: start_date, start_city, finish_city, duration (HH: mm)
My expected output: the best you can reach the finish_city ( in this case 18:40 )
and the trajectory will be ( line 2 -> line 3 -> line 4 )
This is what I implemented so far, I hit a dead-end I know I am supposed to use recursion but not sure how?
const bestTime = (
startTime,
startCity,
finishCity,
listTravelSize,
listTravel
) => {
var startTimeIter;
var startCityIter;
const markTime = [];
const listTravelSecondIter = [];
// check only cities that start with the same start city
const listTravelFiltered = checkCities(listTravel, startCity, startTime);
// recursion part
listTravelFiltered.map((item) => {
// item is of ListTravel type
startTimeIter = sumTime(item.startTime, item.duration);
startCityIter = item.finishCity;
// check if the finish city is the same as the finish city
if (startCityIter === finishCity) {
markTime.push(startTimeIter); // first duration +
}
const travel = {
startTime: startTimeIter,
startCity: startCityIter,
finishCity: finishCity,
duration: item.duration,
};
listTravelSecondIter.push(travel);
});
bestTime(
startTimeIter,
startCityIter,
finishCity,
listTravelSecondIter.length,
listTravelSecondIter
);
};