leetcode

我的 leetcode 题解(JavaScript)


js对算术表达式的处理似乎和其他语言比起来弱了一些,所以该加括号的地方都加上吧

const twoCitySchedCost = function (costs) {
  let res = 0;
  const costsLength = costs.length / 2;
  costs.sort((a, b) => ((a[0] - a[1]) - (b[0] - b[1])));
  for (let i = 0; i < costsLength; ++i) {
    res += (costs[i][0] + costs[i + costsLength][1]);
  }
  return res;
};