Skip to content

Commit 80c3de0

Browse files
committed
feat(algorithms, greedy): two city scheduling
1 parent 510b0ff commit 80c3de0

13 files changed

Lines changed: 132 additions & 0 deletions
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Two City Scheduling
2+
3+
A recruiter plans to hire n people and conducts their interviews at two different locations of the company. He
4+
evaluates the cost of inviting candidates to both these locations. The plan is to invite 50% at one location, and the
5+
rest at the other location, keeping costs to a minimum.
6+
7+
We are given an array, costs, where costs[i]=[aCosti ,bCosti]], the cost of inviting the ith person to City A is aCosti,
8+
and the cost of inviting the same person to City B bCosti.
9+
10+
You need to determine the minimum cost to invite all the candidates for the interview such that exactly n/2 people are
11+
invited in each city.
12+
13+
## Constraints
14+
15+
- 2 <= `costs.length` <= 100
16+
- `costs.length` is even
17+
- 1 <= `aCosti`, `bCosti` <= 1000
18+
19+
## Examples
20+
21+
![Example 1](./images/examples/two_city_scheduling_example_1.png)
22+
![Example 2](./images/examples/two_city_scheduling_example_2.png)
23+
![Example 3](./images/examples/two_city_scheduling_example_3.png)
24+
25+
## Solution
26+
27+
Let’s take an example to understand the solution to this problem. Given the costs array [[100,10],[1000,10],[500,50],[100,1]],
28+
we want to invite half the people to City A and the other half to City B. We can see that inviting the first person and
29+
the fourth person to City A and the second and the third person to City B minimizes the cost to 100 + 100 + 50 + 10 = 260.
30+
31+
Let’s take the difference of all city pairs (aCosti − bCosti) [90,990,450,99]. If we sort our cost array based on these
32+
differences ([90, 99, 450, 990]), then the cost array will be [[100,10],[100,1],[500,50],[1000,10]]. Now, if we invite
33+
the first half of the array to City A and the next half to City B, then the cost is minimized to 100+100+50+10=260.
34+
35+
This algorithm uses a greedy approach that chooses the minimum cost for inviting a person to a city based on the difference
36+
between the cost of inviting them to City A and City B. The idea behind this is that if the cost difference is large,
37+
inviting a person to a city with a lower cost is optimal, because the difference is large. After sorting the array based
38+
on difference, the costs with large differences will be in the second half of the array. When the difference is large,
39+
it means the second value will be much less than the first value in the costs array. Therefore, we are inviting the
40+
second half to City B for optimization.
41+
42+
The steps of the solution according to the above-mentioned methodology are given below:
43+
44+
1. Declare a variable, total_cost, initialized to 0.
45+
2. Sort the costs array in ascending order based on the difference between the cost of sending a person to City A versus
46+
sending the same person to City B.
47+
3. Iterate over the sorted costs array to calculate the minimum costs required to send n/2 people to each city. We will
48+
perform the following steps:
49+
- For the first n/2 elements in the costs array, add aCosti to the total cost for inviting candidates to City A.
50+
- For the remaining n/2 elements in the costs array, add bCosti to the total cost for inviting candidates to City B.
51+
4. Return the total minimum cost after traversing the costs array.
52+
53+
54+
![Solution 1](./images/solutions/two_city_scheduling_solution_1.png)
55+
![Solution 2](./images/solutions/two_city_scheduling_solution_2.png)
56+
![Solution 3](./images/solutions/two_city_scheduling_solution_3.png)
57+
![Solution 4](./images/solutions/two_city_scheduling_solution_4.png)
58+
![Solution 5](./images/solutions/two_city_scheduling_solution_5.png)
59+
![Solution 6](./images/solutions/two_city_scheduling_solution_6.png)
60+
![Solution 7](./images/solutions/two_city_scheduling_solution_7.png)
61+
62+
### Time Complexity
63+
64+
We only traverse the array once but since we use sorting, the time complexity of the solution becomes O(nlog(n)).
65+
66+
### Space Complexity
67+
68+
In Python, the sorting algorithm takes O(n) space to sort the costs array. Therefore, the space complexity of the above
69+
solution is O(n).
70+
71+
## Topics
72+
73+
- Array
74+
- Greedy
75+
- Sorting
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
def two_city_scheduling(costs: List[List[int]]) -> int:
5+
if not costs:
6+
return 0
7+
8+
# Sort by the relative cost difference of sending someone to city A over city B
9+
# Negative or small differences appear first
10+
costs.sort(key=lambda x: x[0] - x[1])
11+
12+
# This gives us the number of people to send to each city. This is equivalent to len(costs) // 2
13+
n = len(costs) >> 1
14+
15+
total_cost = 0
16+
17+
for i in range(n):
18+
total_cost += costs[i][0] + costs[i + n][1]
19+
20+
return total_cost
31.9 KB
Loading
41.4 KB
Loading
41.6 KB
Loading
20.3 KB
Loading
28.4 KB
Loading
28.6 KB
Loading
30 KB
Loading
31.9 KB
Loading

0 commit comments

Comments
 (0)