Advertisement

Two City Scheduling - LeetCode 1029 Solution

Two City Scheduling - Complete Solution Guide

Two City Scheduling is LeetCode problem 1029, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: costs = [[10,20],[30,200],[400,50],[30,20]] Output: 110 Explanation: The first person goes to city A for a cost of 10. The second person goes to city A

Detailed Explanation

The problem asks us to find the minimum cost to send 2n people to two cities, A and B, such that exactly n people are sent to each city. We are given a `costs` array where `costs[i] = [aCost_i, bCost_i]` represents the cost of sending the i-th person to city A and city B, respectively. The goal is to minimize the total cost while ensuring an equal number of people are sent to each city.

Solution Approach

The solution uses a greedy approach. It sorts the `costs` array based on the difference between the cost of going to city A and the cost of going to city B (i.e., `aCost - bCost`). After sorting, the first 'n' people are assigned to city A, and the remaining 'n' people are assigned to city B. This approach minimizes the total cost because we are sending people with the biggest savings (in terms of A vs B cost) to city A first.

Step-by-Step Algorithm

  1. Step 1: Sort the `costs` array in ascending order based on the difference between `aCost` and `bCost` (i.e., `aCost - bCost`).
  2. Step 2: Initialize a variable `total_cost` to 0.
  3. Step 3: Iterate through the first `n` elements of the sorted `costs` array and add the cost of going to city A (`aCost`) to `total_cost`. These people are assigned to city A.
  4. Step 4: Iterate through the next `n` elements of the sorted `costs` array and add the cost of going to city B (`bCost`) to `total_cost`. These people are assigned to city B.
  5. Step 5: Return the `total_cost`.

Key Insights

  • Insight 1: The key is to realize that we need to strategically choose which people to send to which city. Sorting the costs based on the difference between the cost of going to city A and city B is crucial.
  • Insight 2: By sorting the costs based on `aCost - bCost`, we prioritize sending people to city A for whom the cost difference is significantly lower for city A than city B. After sending `n` people to city A, we send the remaining `n` to city B.
  • Insight 3: This greedy approach works because it attempts to minimize the increase in cost by initially prioritizing assignments where the cost difference is most favorable.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy, Sorting.

Companies

Asked at: Swiggy.