All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1001_1100.s1029_two_city_scheduling.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy