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 b6597af

Browse files
author
kkarpyshev
committed
Medium1663 challenge
1 parent e01d157 commit b6597af

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package medium
2+
3+
import ArraysTopic
4+
import GreedyTopic
5+
import SortingTopic
6+
7+
/**
8+
* 1029. Two City Scheduling
9+
* https://leetcode.com/problems/two-city-scheduling/
10+
*
11+
A company is planning to interview 2n people. Given the array costs where costs[i] = [aCosti, bCosti],
12+
the cost of flying the ith person to city a is aCosti, and the cost of flying the ith person to city b is bCosti.
13+
Return the minimum cost to fly every person to a city such that exactly n people arrive in each city.
14+
*/
15+
16+
class Medium1029 : ArraysTopic, GreedyTopic, SortingTopic {
17+
18+
fun twoCitySchedCost(costs: Array<IntArray>): Int {
19+
val sorted = costs.sortedArrayWith(Comparator { a, b -> (a[0] - a[1]) - (b[0] - b[1]) })
20+
var result = 0
21+
for (i in costs.indices) {
22+
result += if (i >= sorted.size / 2) sorted[i][1] else sorted[i][0]
23+
}
24+
return result
25+
}
26+
}
27+
28+
fun main() {
29+
println(
30+
Medium1029().twoCitySchedCost(
31+
arrayOf(
32+
intArrayOf(10, 20),
33+
intArrayOf(30, 200),
34+
intArrayOf(400, 50),
35+
intArrayOf(30, 20)
36+
)
37+
)
38+
)
39+
println(
40+
Medium1029().twoCitySchedCost(
41+
arrayOf(
42+
intArrayOf(259, 770),
43+
intArrayOf(448, 54),
44+
intArrayOf(926, 667),
45+
intArrayOf(184, 139),
46+
intArrayOf(840, 118),
47+
intArrayOf(577, 469)
48+
)
49+
)
50+
)
51+
println(
52+
Medium1029().twoCitySchedCost(
53+
arrayOf(
54+
intArrayOf(515, 563),
55+
intArrayOf(451, 713),
56+
intArrayOf(537, 709),
57+
intArrayOf(343, 819),
58+
intArrayOf(855, 779),
59+
intArrayOf(457, 60),
60+
intArrayOf(650, 359),
61+
intArrayOf(631, 42)
62+
)
63+
)
64+
)
65+
}

0 commit comments

Comments
(0)

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