Two City Scheduling

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

A company is planning to interview 2n people. Given the array costs where costs[i] = [aCost_i, bCost_i], the cost of flying the i-th person to city A is aCost_i and the cost of flying the i-th person to city B is bCost_i.

Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.

Examples

Example 1:

Input: costs = [[10,20],[30,200],[400,50],[30,20]]

Output: 110

Explanation: Person 0 goes to city A (cost 10), person 1 goes to city A (cost 30), person 2 goes to city B (cost 50), person 3 goes to city B (cost 20). Total = 10 + 30 + 50 + 20 = 110.

Example 2:

Input: costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]

Output: 1859

Explanation: The optimal assignment sends persons 0, 3, 5 to city A and persons 1, 2, 4 to city B.

Example 3:

Input: costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]

Output: 3086

Explanation: Send persons 0, 2, 3, 6 to city A and persons 1, 4, 5, 7 to city B for minimum total cost.

Constraints

  • 2 * n == costs.length
  • 2 <= costs.length <= 100
  • costs.length is even
  • 1 <= aCost_i, bCost_i <= 1000
Source: Greedy Techniques pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle