Bird
Raised Fist0

What is the time complexity of the optimal greedy solution for Two City Scheduling that sorts by cost difference and assigns people in a single pass?

medium🪤 Complexity Trap Q13 of Q15
Greedy Algorithms - Two City Scheduling
What is the time complexity of the optimal greedy solution for Two City Scheduling that sorts by cost difference and assigns people in a single pass?
AO(n^2) because of nested loops to assign people
BO(n log n) due to sorting the list of 2n people
CO(n) since assignment is done in one pass after sorting
DO(n log n) including sorting and constant time assignment
Step-by-Step Solution
Solution:
  1. Step 1: Identify sorting cost

    Sorting 2n people by cost difference takes O(n log n) time.
  2. Step 2: Identify assignment cost

    Assigning people in a single pass is O(n).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting dominates, total complexity is O(n log n) [OK]
Quick Trick: Sorting dominates time complexity [OK]
Common Mistakes:
MISTAKES
  • Assuming assignment is nested loop causing O(n^2)
  • Ignoring sorting cost and claiming O(n)
  • Confusing space complexity with time complexity
Trap Explanation:
PITFALL
  • Option B looks right but omits the assignment step; option D correctly includes both sorting and assignment.
Interviewer Note:
CONTEXT
  • Checks understanding of sorting cost and linear assignment in greedy algorithms.
Master "Two City Scheduling" in Greedy Algorithms

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Greedy Algorithms Quizzes