Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 99b5efb

Browse files
solve: 1029. Two City Scheduling in rustlang
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent bff5ec7 commit 99b5efb

File tree

1 file changed

+21
-0
lines changed

1 file changed

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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /