Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 700 Bytes

graph-business-trip.md

File metadata and controls

33 lines (25 loc) · 700 Bytes

Implementation: Graph business trip

Whiteboard Process

White-Borad Link

Solution

"use strict";
function businessTrip(graph, cities) {
  let cost = 0;

  for (let i = 0; i < cities.length - 1; i++) {
    const currentCity = cities[i];
    const nextCity = cities[i + 1];
    const neighbors = graph.getNeighbors(currentCity);

    const directFlight = neighbors.find(
      (neighborEdge) => neighborEdge.vertex === nextCity
    );

    if (directFlight) {
      cost += directFlight.weight;
    } else {
      return null;
    }
  }

  return cost;
}