Skip to content

Commit d790f5b

Browse files
Question 1029 has been solved.
1 parent 02b0a08 commit d790f5b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

0 commit comments

Comments
 (0)