Greedy Algorithms8 sections · 316 units
Open in Course

Problem - Two City Scheduling

Split evenly

A company needs to fly 2n2n people to two cities, nn to each city. Person ii costs costs[i][0]costs[i][0] to fly to city A and costs[i][1]costs[i][1] to city B. Minimize total cost. For costs=[[10,20],[30,200],[400,50],[30,20]]costs = [[10,20],[30,200],[400,50],[30,20]], send persons 00 and 11 to A, persons 22 and 33 to B. Total: 10+30+50+20=11010 + 30 + 50 + 20 = 110.

Think about the cost difference between sending someone to A versus B. Constraint: exactly nn people must go to each city.

If you send fewer than nn to city A, you violate the requirement. The cost array has two elements per person, representing the two choices. The greedy insight is to sort by opportunity cost, not absolute cost.