19. Bus Conductor
β GFG solution to the Bus Conductor problem: find minimum moves to assign passengers to chairs using greedy sorting approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are a conductor of a bus. You are given two arrays chairs[] and passengers[] of equal length, where chairs[i] is the position of the ith chair and passengers[j] is the position of the jth passenger. You may perform the following move any number of times:
Increase or decrease the position of the ith passenger by 1 (i.e., moving the ith passenger from position x to x+1 or x-1)
Return the minimum number of moves required to move each passenger to get a chair.
Note: Although multiple chairs can occupy the same position, each passenger must be assigned to exactly one unique chair.
π Examples
Example 1
Input: chairs[] = [3, 1, 5], passengers[] = [2, 7, 4]
Output: 4
Explanation: The passengers are moved as follows:
- The first passenger is moved from position 2 to position 1 using 1 move.
- The second passenger is moved from position 7 to position 5 using 2 moves.
- The third passenger is moved from position 4 to position 3 using 1 move.
In total, 1 + 2 + 1 = 4 moves were used.Example 2
π Constraints
$1 \le n \le 10^5$
$1 \le \text{chairs}[i], \text{passengers}[j] \le 10^4$
β
My Approach
The optimal solution uses a Greedy Sorting strategy to minimize total moves:
Greedy Sorting Strategy
Key Insight:
To minimize total distance, we should pair the smallest chair position with the smallest passenger position, second smallest with second smallest, and so on.
This greedy approach ensures optimal assignment without considering all possible permutations.
Sort Both Arrays:
Sort
chairs[]in ascending order to arrange chair positions from smallest to largest.Sort
passengers[]in ascending order to arrange passenger positions from smallest to largest.
Calculate Minimum Moves:
After sorting, iterate through both arrays simultaneously.
For each index i, calculate the absolute difference:
|chairs[i] - passengers[i]|.This difference represents the minimum moves needed to move passenger i to chair i.
Accumulate Total:
Sum all individual move distances to get the total minimum moves required.
Why This Works:
Sorting ensures we're making locally optimal choices at each step.
Pairing nearest positions minimizes crossing paths and redundant movements.
The greedy choice property guarantees global optimality for this problem.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), as we perform sorting on both arrays which dominates the complexity. The subsequent linear traversal to calculate moves takes O(n), but sorting is the bottleneck.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables (res, n, i). The sorting is typically done in-place or with O(log n) stack space for recursion, which is considered constant for practical purposes.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated