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

g0801_0900.s0851_loud_and_rich.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0801_0900.s0851_loud_and_rich

// #Medium #Array #Depth_First_Search #Graph #Topological_Sort
// #2023_03_30_Time_347_ms_(100.00%)_Space_54.4_MB_(80.00%)

class Solution {
    fun loudAndRich(richer: Array, quiet: IntArray): IntArray {
        val result = IntArray(quiet.size)
        for (i in quiet.indices) {
            result[i] = i
        }
        for (k in quiet.indices) {
            var changed = false
            for (r in richer) {
                if (quiet[result[r[0]]] < quiet[result[r[1]]]) {
                    result[r[1]] = result[r[0]]
                    changed = true
                }
            }
            if (!changed) {
                break
            }
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy