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 one pass?

medium🪤 Complexity Trap Q5 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 one pass?
AO(2^n) exponential due to assignment combinations
BO(n log n) due to sorting costs by difference
CO(n^2) due to nested loops for assignment
DO(n) linear since only one pass is needed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze sorting step

    Sorting costs by difference takes O(n log n) time.
  2. Step 2: Analyze assignment step

    Single pass assignment is O(n), dominated by sorting.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sorting dominates time complexity [OK]
Quick Trick: Sorting dominates time, assignment is linear [OK]
Common Mistakes:
MISTAKES
  • Confusing with exponential brute force
  • Assuming linear only
  • Mistaking nested loops for assignment
Trap Explanation:
PITFALL
  • Candidates often forget sorting cost and pick linear or quadratic incorrectly.
Interviewer Note:
CONTEXT
  • Tests understanding of time complexity in greedy approach
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