Greedy Algorithms - Two City SchedulingGiven the following code and input, what is the returned total cost?A120B110C150D160Check Answer
Step-by-Step SolutionSolution:Step 1: Sort costs by difference (costA - costB)Sorted order: [10,20], [30,20], [400,50], [30,200]Step 2: Assign first n=2 to city A, rest to city B and sum costsCity A: 10 + 30 = 40; City B: 50 + 20 = 70; Total = 110Final Answer:Option B -> Option BQuick Check:Correct sorting and assignment yields 110 [OK]Quick Trick: Sort by cost difference, assign first half to city A [OK]Common Mistakes:MISTAKESNot sorting by differenceAssigning wrong half to city AOff-by-one in loopTrap Explanation:PITFALLCandidates often forget to sort or misassign halves causing wrong totals.Interviewer Note:CONTEXTTests ability to trace greedy code on canonical input
Master "Two City Scheduling" in Greedy Algorithms3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Greedy Algorithms Quizzes Assign Cookies - Assign Cookies - Quiz 3easy Best Time to Buy and Sell Stock II - Best Time to Buy and Sell Stock II - Quiz 6medium Candy Distribution - Candy Distribution - Quiz 12easy Jump Game (Can Reach End?) - Jump Game (Can Reach End?) - Quiz 5medium Jump Game (Can Reach End?) - Jump Game (Can Reach End?) - Quiz 1easy Largest Number (Arrange to Form Biggest) - Largest Number (Arrange to Form Biggest) - Quiz 7medium Minimum Domino Rotations - Minimum Domino Rotations - Quiz 8hard Minimum Platforms (Train Stations) - Minimum Platforms (Train Stations) - Quiz 4medium Wiggle Subsequence - Wiggle Subsequence - Quiz 13medium Wiggle Subsequence - Wiggle Subsequence - Quiz 12easy