【算法题】开源项目热度榜单(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

 */
相关推荐
郑州光合科技余经理7 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1237 小时前
matlab画图工具
开发语言·matlab
唐璜Taro7 小时前
Vue3 + TypeScript 后台管理系统完整方案
前端·javascript·typescript
dustcell.7 小时前
haproxy七层代理
java·开发语言·前端
norlan_jame8 小时前
C-PHY与D-PHY差异
c语言·开发语言
ZPC82108 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82108 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David8 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
颜酱8 小时前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
Xin_z_8 小时前
Vue3 + Sticky 锚点跳转被遮挡问题解决方案
前端·javascript·vue.js