g1001_1100.s1029_two_city_scheduling.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1001_1100.s1029_two_city_scheduling
// #Medium #Array #Sorting #Greedy #2023_05_24_Time_148_ms_(100.00%)_Space_35.4_MB_(92.31%)
import java.util.Arrays
class Solution {
fun twoCitySchedCost(costs: Array): Int {
Arrays.sort(costs) { a: IntArray, b: IntArray ->
a[0] - a[1] - (b[0] - b[1])
}
var cost = 0
for (i in costs.indices) {
cost += if (i < costs.size / 2) {
costs[i][0]
} else {
costs[i][1]
}
}
return cost
}
}