1029. Two City Scheduling

A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti], the cost of flying the ith person to city a is aCosti, and the cost of flying the ith person to city b is bCosti. Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.

by Abhishek Kumar

JavaScript

/**
 * @param {number[][]} costs
 * @return {number}
 */
var twoCitySchedCost = function(costs) {
  let output = 0, costsLen = costs.length;
  if (costsLen % 2 == 0 && 2 <= costsLen && costsLen <= 100) {
    for (let i = 0; i < costsLen; i++) {
		  let aCost = costs[i][0], bCost = costs[i][1];
      if ((1 <= aCost && aCost <= 1000) && (1 <= bCost && bCost <= 1000)) {
        output += (aCost < bCost) ? aCost : bCost;
      }
    }
  }
  return output;
};

console.log(twoCitySchedCost([
  [10, 20],
  [30, 200],
  [400, 50],
  [30, 20]
]))
console.log(twoCitySchedCost([
  [259, 770],
  [448, 54],
  [926, 667],
  [184, 139],
  [840, 118],
  [577, 469]
]))
console.log(twoCitySchedCost([
  [515, 563],
  [451, 713],
  [537, 709],
  [343, 819],
  [855, 779],
  [457, 60],
  [650, 359],
  [631, 42]
]))