xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 2285. Maximum Total Importance of Roads All In One

LeetCode 2285. Maximum Total Importance of Roads All In One

difficulty: Medium / 难度: 中等

LeetCode 2285 道路的重要性最大和

errors


solution

Map

// bidirectional 双向
function maximumImportance(n: number, roads: number[][]): number {
  // Map 排序,连接最多的 city,赋值最大 n; 依次递减
  let cities = new Map();
  for(let arr of roads) {
    for(let n of arr) {
      if(cities.has(n)) {
        cities.set(n, {n, value: 0, road: cities.get(n).road += 1});
      } else {
        cities.set(n, {n, value: 0, road: 1});
      }
    }
  }
  // 排序
  const arr = [...cities.values()].sort((a, b) => a.road - b.road);
  // 先使用 n 的最大值 ✅
  let num = n;
  for(let i = arr.length; i > 0; i--) {
    arr[i - 1].value = num--;
  }
  for(let i = 0; i < arr.length; i++) {
    const {n} = arr[i];
    cities.set(n, arr[i]);
  }
  // 累加求和
  let sum = 0;
  for(let arr of roads) {
    const [a, b] = arr;
    sum += cities.get(a).value;
    sum += cities.get(b).value;
  }
  return sum;
};


image

demos

// bidirectional 双向
function maximumImportance(n: number, roads: number[][]): number {
  // Map 排序
  // 连接最多的 city,赋值最大 n; 依次递减
  // 累加求和 ()
  let cities = new Map();
  for(let arr of roads) {
    for(let n of arr) {
      if(cities.has(n)) {
        cities.set(n, {n, value: 0, road: cities.get(n).road += 1});
      } else {
        cities.set(n, {n, value: 0, road: 1});
      }
    }
  }
  // 排序 bug 
  // [...cities.entries()].sort((a, b) => a[1].road - b[1].road);
  const arr = [...cities.values()].sort((a, b) => a.road - b.road);
  // console.log(`cities =`, cities)
  // for(let i = 1; i <= n; i++) {
  //   arr[i - 1].value = i;
  // }
  // 先使用最大值
  let num = n;
  for(let i = arr.length; i > 0; i--) {
    arr[i - 1].value = num--;
  }
  // let num = 1;
  // for(let i = 0; i < arr.length; i++) {
  //   arr[i].value = num++;
  //   // arr[i].value = num;
  //   // num++;
  // }
  for(let i = 0; i < arr.length; i++) {
    // const {n} = arr[i];
    // cities.set(n, arr[i]);
    const {n, value, road} = arr[i];
    cities.set(n, {n, value, road});
  }
  // console.log(`cities =`, cities)
  let sum = 0;
  for(let arr of roads) {
    const [a, b] = arr;
    sum += cities.get(a).value;
    sum += cities.get(b).value;
  }
  return sum;
};


/* 

Wrong Answer
41 / 58 testcases passed

Input
n =
5
roads =
[[0,1]]

Output
3
Expected
9

 */

// // bidirectional 双向
// function maximumImportance(n: number, roads: number[][]): number {
//   // Map 排序
//   // 连接最多的 city,赋值最大 n; 依次递减
//   // 累加求和
//   // let cities = new Array(n).fill(0);
//   let cities = new Array(n).fill(0).map((_, i) => ({n: i, value: 0, road: 0}));
//   for(let arr of roads) {
//     const [a, b] = arr;
//     cities[a].road += 1;
//     cities[b].road += 1;
//   }
//   cities.sort((a, b) => a.road - b.road);
//   // cities.sort((a, b) => a.road - b.road > 0 ? -1 : 1);
//   // console.log(`cities =`, cities)
//   for(let i = 1; i <= n; i++) {
//     cities[i - 1].value = i; 
//   }
//   // console.log(`cities =`, cities)
//   let sum = 0;
//   for(let arr of roads) {
//     const [a, b] = arr;
//     sum += cities.find(obj => obj.n === a).value;
//     sum += cities.find(obj => obj.n === b).value;
//   }
//   return sum;
// };

/* 

您需要为每个城市分配一个从 1 到 n 的整数值,其中每个值只能使用一次。然后,道路的重要性被定义为它所连接的两个城市的价值之和。

以最佳方式分配值后,返回所有可能道路的最大总重要性。

 */


 /* 
 
Time Limit Exceeded
52 / 58 testcases passed

Last Executed Input

n =
40654

roads =
[[22984,20264],[20264,14202],[14202,8684],
...
9953,22132],[22132,22753],[22753,12166]]

  */

https://leetcode.com/problems/maximum-total-importance-of-roads/description/?envType=daily-question&envId=2024-06-28

LeetCode 2285. 道路的最大总重要性

https://leetcode.cn/problems/maximum-total-importance-of-roads

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://www.cnblogs.com/xgqfrms/p/14182098.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-06-29 10:24  xgqfrms  阅读(5)  评论(0编辑  收藏  举报