【算法题】开源项目热度榜单(js)





解法

js 复制代码
const lines = [
  "4",
  "8 6 2 8 6",
  "camila 66 70 46 158 80",
  "victoria 94 76 86 189 211",
  "athony 29 17 83 21 48",
  "emily 53 97 1 19 218",
];
const lines2 = [
  "5",
  "5 6 6 1 2",
  "camila 13 88 46 26 169",
  "grace 64 38 87 23 103",
  "lucas 91 79 98 154 79",
  "leo 29 27 36 43 178",
  "ava 29 27 36 43 178",
];

function solution(lines) {
  const personNum = parseInt(lines[0]);
  const weights = lines[1].split(" ").map((item) => parseInt(item));
  const projects = [];
  function countHot(weights, list) {
    console.log(weights, list);
    let sum = 0;
    for (let j = 0; j < weights.length; j++) {
      sum += weights[j] * list[j];
    }
    return sum;
  }
  for (let i = 2; i < 2 + personNum; i++) {
    const arr = lines[i].split(" ");
    const name = arr.shift();
    const list = arr.map((item) => parseInt(item));
    const hot = countHot(weights, list);
    projects.push({
      name,
      list,
      hot,
    });
  }
  console.log("projects", projects);
  const results = projects.sort((a, b) =>
    a.hot !== b.hot ? b.hot - a.hot : a.name.localeCompare(b.name)
  );
  console.log("results", results);
  return results.map((item) => item.name).join("\n");
}

console.log(solution(lines2));
/* 
    "4",
    "8 6 2 8 6",
    "camila 66 70 46 158 80",
    "victoria 94 76 86 189 211",
    "athony 29 17 83 21 48",
    "emily 53 97 1 19 218",
=>
victoria
camila
emily
athony


    "5",
    "5 6 6 1 2",
    "camila 13 88 46 26 169",
    "grace  64 38 87 23 103",
    "lucas 91 79 98 154 79",
    "leo 29 27 36 43 178",
    "ava 29 27 36 43 178",
=>
 lucas
 grace
 camila
 ava
 leo

 */
相关推荐
skaiuijing5 分钟前
Sparrow系列拓展篇:消息队列和互斥锁等IPC机制的设计
c语言·开发语言·算法·操作系统·arm
小镇程序员38 分钟前
vue2 src自定义事件
前端·javascript·vue.js
炒毛豆1 小时前
vue3+echarts+ant design vue实现进度环形图
javascript·vue.js·echarts
雯0609~2 小时前
c#:winform调用bartender实现打印(学习整理笔记)
开发语言·c#
C++忠实粉丝2 小时前
计算机网络socket编程(5)_TCP网络编程实现echo_server
网络·c++·网络协议·tcp/ip·计算机网络·算法
kim56592 小时前
excel版数独游戏(已完成)
算法·游戏·excel·数独
cv君3 小时前
【AI最前线】DP双像素sensor相关的AI算法全集:深度估计、图像去模糊去雨去雾恢复、图像重建、自动对焦
算法
胜天半子_王二_王半仙3 小时前
c++源码阅读__smart_ptr__正文阅读
开发语言·c++·开源
Ocean☾3 小时前
C语言-详细讲解-P1217 [USACO1.5] 回文质数 Prime Palindromes
c语言·数据结构·算法
沐泽Mu3 小时前
嵌入式学习-C嘎嘎-Day08
开发语言·c++·算法