Two City Scheduling

A company plans to interview 2n people. costs[i] = [aCost_i, bCost_i] is the cost of flying person i to city a or to city b. Return the minimum total cost to fly every person to a city so that exactly n people arrive in each city.

30:00

Two City Scheduling
medium
Free written solution
Topics
Companies

A company plans to interview 2n people. costs[i] = [aCost_i, bCost_i] is the cost of flying person i to city a or to city b. Return the minimum total cost to fly every person to a city so that exactly n people arrive in each city.

Example 1:
Input: {"costs":[[10,20],[30,200],[400,50],[30,20]]}
Output: 110
Constraints:
  • 2n=costs.length2n = \text{costs.length}

  • 2≤costs.length≤1002 \leq \text{costs.length} \leq 100

  • costs.length\text{costs.length} is even

  • 1≤aCosti,bCosti≤10001 \leq aCost_i, bCost_i \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"costs":[[10,20],[30,200],[400,50],[30,20]]}

4 people, each with a price to fly to city A and to city B. Exactly half must go to each city.

city A

0/?

cost 0

city B

0/?

cost 0

a - b per person, input order

below 0: A is cheaper

-10

#0

-170

#1

350

#2

10

#3

total so far

0

Variables
VariableValue
n0
total0
DepthFunction Call
Stack empty
0/8