1/**2* @param {number[][]} costs3* @return {number}4*/5var twoCitySchedCost = function (costs) {6// TC: O(nlogn) and O(1) extra space7let n = costs.length;8let countA = 0,9countB = 0,10minCost = 0;1112// sorted in descending order by their absolute diff13costs = costs.sort((a, b) => {14let diffA = Math.abs(a[0] - a[1]);15let diffB = Math.abs(b[0] - b[1]);16return diffB - diffA;17});1819for (let i = 0; i < n; i++) {20let [a, b] = costs[i];21if (a < b) {22if (countA < n / 2) {23minCost += a;24countA++;25} else {26minCost += b;27countB++;28}29} else {30if (countB < n / 2) {31minCost += b;32countB++;33} else {34minCost += a;35countA++;36}37}38}39return minCost;40};