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

 */
相关推荐
yugi98783822 分钟前
MATLAB的多层感知器(MLP)与极限学习机(ELM)实现
开发语言·matlab
Never_Satisfied1 小时前
C#获取汉字拼音字母方法总结
开发语言·c#
弓.长.1 小时前
React Native 鸿蒙跨平台开发:实现一个多功能单位转换器
javascript·react native·react.js
zh_xuan1 小时前
kotlin 密封类
开发语言·kotlin
码小猿的CPP工坊1 小时前
C++软件开发之内存泄漏闭坑方法
开发语言·c++
摘星编程1 小时前
React Native for OpenHarmony 实战:ToggleSwitch 切换开关详解
javascript·react native·react.js
Ethan-D1 小时前
#每日一题19 回溯 + 全排列思想
java·开发语言·python·算法·leetcode
Benny_Tang1 小时前
题解:CF2164C Dungeon
c++·算法
仙俊红1 小时前
LeetCode174双周赛T3
数据结构·算法
满栀5852 小时前
分页插件制作
开发语言·前端·javascript·jquery