Skip to content
BytePatterns

Split Candidates Between Two Cities

EasyGreedy#greedy#sort-by-difference~20m

Problem

A company is flying an even number of candidates to on-site interviews, and exactly half must go to office A and half to office B. costs[i] = [a, b] gives the fare for candidate i to each office. Return the lowest total fare that sends exactly half of the candidates to each office.

Examples

Input:  costs = [[10, 20], [30, 200], [400, 50], [30, 20]]
Output: 110
Why:    the first two go to A for 10 + 30, the last two to B for 50 + 20
Input:  costs = [[259, 770], [448, 54], [926, 667], [184, 139], [840, 118], [577, 469]]
Output: 1859
Input:  costs = [[5, 5], [7, 7]]
Output: 12
Why:    edge case, both offices cost the same, so any even split is optimal

Hints

0 / 3

Stuck on the idea rather than the code? Sorting Basics covers it.