javascript - source, destination, distance - 计算距离最长的源和目的地

标签 javascript algorithm data-structures

我正在尝试解决计算任意两点之间的最大距离的问题。

我希望输出能够计算行进距离最长的源和目标对。

      obj = [{
                source: a,
                destination: b,
                distance: 200
              },
              {
                source: b,
                destination: a,
                distance: 100
              },
              {
                source: a,
                destination: c,
                distance: 100
              }
            ]     

在这种情况下,我的输出将是 highestDistance = [a, b, 300] ( between a and b => 200 + 100 = 300)

我正在尝试用 javascript 编写一个函数。什么数据结构适合这里?

我最初尝试创建一个 map 并将 [source, destination] 元组添加为键,如下所示:

 {
   [a, b]: 200,
   [a, c]: 100
 }

const obj = [{
                source: a,
                destination: b,
                distance: 200
              },
              {
                source: b,
                destination: a,
                distance: 100
              },
              {
                source: a,
                destination: c,
                distance: 100
              }
            ]

    function highestDistance(obj) {
          const highestPair = obj[0];
          const myMap = new Map();
          obj.forEach(pair => {
            let [source, destination] = [pair.source, pair.destination];
            if( myMap.has([source, destination]) || myMap.has([source, destination])){
                myMap.set() 
                 // not sure how to proceed and add the tuple to map here
                // I intend to compare the current highest and update highestPair value if the current pair distance is collectively bigger.
            } else {
              myMap.set([source, destination], pair[distance])
           }

          })

          return obj;
        }

输入:

      {
        source: a,
        destination: b,
        distance: 200
      },
      {
        source: b,
        destination: a,
        distance: 100
      },
      {
        source: a,
        destination: c,
        distance: 100
      }

输出:

[a, b, 300]

你能帮我解决这个问题吗?非常感谢!

最佳答案

您可以创建具有距离的对象。稍后您需要找到最大距离。

var obj = [{
                source: 'a',
                destination: 'b',
                distance: 200
              },
              {
                source: 'b',
                destination: 'a',
                distance: 100
              },
              {
                source: 'a',
                destination: 'c',
                distance: 100
              }
            ]

var distMap = obj.reduce((f, n)=> {
  const {source,destination, distance } = n;
  const key  = `${source}->${destination}`
  const key2 = `${destination}->${source}`

  if(f[key2]){
    f[key2] = distance+ f[key2]
  } else {
    f[`${source}->${destination}`] = distance
  }
  return f
}, {}) // {a->b: 300, a->c: 100}

关于javascript - source, destination, distance - 计算距离最长的源和目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56536768/

相关文章:

java - 将二维矩形网格拆分为更小网格的算法

javascript - 将 Array.prototype.sort() 与比较函数一起使用时的排序算法是什么

java - 为什么 "Clone a linked list with next and random pointer"的这个解的空间复杂度是 O(1) ?

data-structures - 实践中哪个优先级队列更快?

javascript - Jquery 工具提示图像高度和宽度

javascript - 变量作为 JavaScript 对象文字中的属性名称?

javascript - 如何使具有许多细微差别的 if-s 的代码更短?

javascript - 如何强制按钮自动执行某些操作?

c - 多数表决算法 - 错误?

algorithm - 高效查找二维中某个点支配的所有点