We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bff5ec7 commit 99b5efbCopy full SHA for 99b5efb
src/1001-1100/1029 - Two City Scheduling/two_city_scheduling.rs
@@ -0,0 +1,21 @@
1
+impl Solution {
2
+ pub fn two_city_sched_cost(costs: Vec<Vec<i32>>) -> i32 {
3
+ let mut costs = costs;
4
+
5
+ let mut total = 0;
6
+ let n = costs.len() / 2;
7
8
+ // sort by a gain which company has
9
+ // by sending a person to city A and not to city B
10
+ costs.sort_by(|a, b| (a[0] - a[1]).cmp(&(b[0] - b[1])));
11
12
+ // to optimize the company expenses,
13
+ // send the first n persons to the city A
14
+ // and the others to the city B
15
+ for i in 0..n {
16
+ total += costs[i][0] + costs[i + n][1]; // city A + city B
17
+ }
18
19
+ total
20
21
+}
0 commit comments