File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Q-1029 - Two City Scheduling Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ bool compare (vector<int >& a, vector<int >& b){
2+ return a[0 ]-a[1 ] < b[0 ]-b[1 ];
3+ }
4+
5+ class Solution {
6+ public:
7+ int twoCitySchedCost (vector<vector<int >>& costs) {
8+
9+ int minSum=0 ;
10+ sort (costs.begin (), costs.end (), compare);
11+
12+ for (int i=0 ;i<costs.size ();i++){
13+ if (i<costs.size ()/2 )
14+ minSum+=costs[i][0 ];
15+ else
16+ minSum+=costs[i][1 ];
17+ }
18+
19+ return minSum;
20+ }
21+ };
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def twoCitySchedCost (self , costs : List [List [int ]]) -> int :
3+
4+ minSum = 0
5+ costs = sorted (costs , key = lambda interval : interval [0 ] - interval [1 ])
6+
7+ for i in range (len (costs )):
8+ if (i < len (costs )/ 2 ) :
9+ minSum += costs [i ][0 ]
10+ else :
11+ minSum += costs [i ][1 ]
12+
13+ return minSum
You can’t perform that action at this time.
0 commit comments