【问题标题】:Lowest Total Distance Possible Parking Lot Algorithm可能的最短总距离停车场算法
【发布时间】:2020-10-29 00:42:41
【问题描述】:

今天我参加了最后一轮算法编码面试,最后被问到我的最后一个问题难住了。与其让它继续我的生活,我真的很想看看解决方案会是什么样子。我会尽力描述它:

你有一组汽车和停车场。

您知道每辆车到每个停车场的距离以及停车场的容量。

您将如何确保每辆车到达一个有空位的停车场,以使所有车辆行驶的总距离尽可能短?

一个小例子可能是:

distance_car1_to_lot1 = 1
distance_car1_to_lot2 = 10
distance_car2_to_lot1 = 2
distance_car2_to_lot2 = 100
lot1_capacity = 1
lot2_capacity = 1

您可以在这里看到,如果您将car1 放入lot1car2 放入lot2,则所有汽车的行驶距离将为101

而不是这样做,将car1 转换为lot2 并将car2 转换为lot1 将导致总行程距离为12

假设汽车和停车场比上面列出的更多,你会如何解决这个问题。

我在整个面试过程中选择的语言是 Javascript,但我想你可以做最适合你的事情。

【问题讨论】:

    标签: javascript algorithm dynamic-programming


    【解决方案1】:

    您可以将这个问题写成一个图,其中顶点是汽车和停车场,边权重是行驶距离。该图是一个完整的二分加权图。您可以使用匈牙利算法 (Kuhn-Munkres) 找到汽车到停车场的最佳分配,如以下视频所述:https://www.youtube.com/watch?v=FCaD34z--bY

    要处理停车场容量,您可以将停车场节点拆分为容量为 1 的多个节点。例如,如果停车场 2 的容量为 3,则可以将其替换为 Lot 2a、2b 和 2c。 您可能还需要添加虚拟节点来平衡汽车和停车场的数量。例如,如果您有 3 个汽车节点和 4 个停车场节点,您可以将 1 个边权重为 0 的虚拟汽车节点添加到所有停车场节点。

    【讨论】:

      【解决方案2】:

      这就是我现在愿意做的所有事情。祝你好运!

      function distance(lat1, lng1, lat2, lng2, feet = false){
        let p = 0.017453292519943295, c = Math.cos;
          let r = 12742000*Math.asin(Math.sqrt(0.5-c((lat1-lat2)*p)/2+c(lat2*p)*c(lat1*p)*(1-c((lng1-lng2)*p))/2));
          if(feet)r *= 1/0.3048;
          return r
      }
      function Vehicle(color, year, make, model, lat = null, lng = null){
        this.color = color; this.year = year; this.make = make; this.model = model; this.lat = lat; this.lng = lng;
        this.distanceTo = (latLng, feet = false)=>{
          return distance(this.lat, this.lng, latLng.lat, latLng.lng, feet);
        }
      }
      function Lot(lat, lng, capacity){
        this.lat = lat; this.lng = lng; this.capacity = capacity;
        this.vehicles = [];
        this.addVehicle = vehicle=>{
          if(this.vehicles.length < this.capacity){
            vehicle.lat = this.lat; vehicle.lng = this.lng; this.vehicles.push(vehicle);
          }
          return this;
        }
        this.removeVehicle = vehicle=>{
          const v = this.vehicles, x = v.indexOf(vehicle);
          if(x !== -1)v.splice(x, 1);
          return this;
        }
        this.distanceTo = (latLng, feet = false)=>{
          return distance(this.lat, this.lng, latLng.lat, latLng.lng, feet);
        }
      }
      function sortDistances(distances, feet = false){
        const d = [...distances], r = [], sortIt = (a, b)=>Object.values(a)[0]-Object.values(b)[0];
        let n, a;
        for(let lot of d){
          n = -1; a = [];
          for(let l of d){
            n++;
            if(l !== lot)a.push({[n]:lot.distanceTo(l, feet)});
          }
          a.sort(sortIt); r.push(a);
        }
        return r;
      }
      const lot1 = new Lot(47.581148063351534, -122.24032639373426, 1);
      const lot2 = new Lot(47.583444521820496, -122.2447443008423, 1);
      const lot3 = new Lot(47.583544521820333, -122.2447543008222, 35);
      const vehicle1 = new Vehicle('yellow', 1979, 'Ford', 'Pinto');
      const vehicle2 = new Vehicle('red', 2020, 'Ferrari', '812 Superfast');
      lot1.addVehicle(vehicle1); lot2.addVehicle(vehicle2);
      const lots = [lot1, lot2, lot3]; // so you can get the lots by index later
      const d = sortDistances(lots);
      console.log(d); // an Array of Arrays of Objects with lots indexes as keys and meters as values

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        相关资源
        最近更新 更多