This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1029. Two City Scheduling, the solution in this repository is mainly a greedy solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/two-city-scheduling/discuss/278716/C%2B%2B-O(n-log-n)-sort-by-savings
- time: O(nlogn), space: O(1)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are twoCitySchedCost.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(nlogn), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/two-city-scheduling/discuss/278716/C%2B%2B-O(n-log-n)-sort-by-savings
02//time: O(nlogn), space: O(1)
03//Runtime: 4 ms, faster than 89.86% of C++ online submissions for Two City Scheduling.
04//Memory Usage: 9.3 MB, less than 94.74% of C++ online submissions for Two City Scheduling.
05
06class Solution {
07public:
08 int twoCitySchedCost(vector<vector<int>>& costs) {
09 //costs.size() is always even
10 int N = costs.size()/2;
11 int ans = 0;
12 //the first N element will be the elements whose
13 //A_cost - B_cost is smaller,
14 //they should fly to A
15 sort(costs.begin(), costs.end(),
16 [](vector<int> &v1, vector<int> &v2) {
17 return v1[0] - v1[1] < v2[0] - v2[1];
18 });
19 for(int i = 0; i < N; i++){
20 ans += costs[i][0] + costs[i+N][1];
21 }
22 return ans;
23 }
24};
25
26//optimized
27//Runtime: 4 ms, faster than 89.86% of C++ online submissions for Two City Scheduling.
28//Memory Usage: 9.3 MB, less than 94.74% of C++ online submissions for Two City Scheduling.
29
30class Solution {
31public:
32 int twoCitySchedCost(vector<vector<int>>& costs) {
33 //costs.size() is always even
34 int N = costs.size()/2;
35 int ans = 0;
36 //the first N element will be the elements whose
37 //A_cost - B_cost is smaller,
38 //they should fly to A
39 nth_element(costs.begin(), costs.begin() + N,
40 costs.end(), [](vector<int>& v1, vector<int>& v2){
41 return v1[0] - v1[1] < v2[0] - v2[1];
42 });
43 for(int i = 0; i < N; i++){
44 ans += costs[i][0] + costs[i+N][1];
45 }
46 return ans;
47 }
48};
Cost