Skip to content

Commit 82074ec

Browse files
authored
Two city scheduling
1 parent 5b42185 commit 82074ec

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Two City Scheduling

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
static bool comp(vector<int> a, vector<int> b)
4+
{
5+
return (abs(a[0]-a[1]) > abs(b[0]-b[1]));
6+
}
7+
8+
int twoCitySchedCost(vector<vector<int>>& costs)
9+
{
10+
int ans = 0;
11+
sort(costs.begin(), costs.end(), comp);
12+
for(int i=0;i<costs.size();i++)
13+
cout<<costs[i][0]<<" "<<costs[i][1]<<"\n";
14+
int n=costs.size();
15+
int a = n/2, b = n/2;
16+
17+
for(int i=0;i<costs.size();i++)
18+
{
19+
if(b==0 || (costs[i][0] <= costs[i][1] and a>0))
20+
{
21+
ans += costs[i][0];
22+
a--;
23+
}
24+
else
25+
{
26+
ans += costs[i][1];
27+
b--;
28+
}
29+
}
30+
cout<<a<<" "<<b
31+
; return ans;
32+
}
33+
34+
};

0 commit comments

Comments
 (0)