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

g0601_0700.s0646_maximum_length_of_pair_chain.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0601_0700.s0646_maximum_length_of_pair_chain

// #Medium #Array #Dynamic_Programming #Sorting #Greedy
// #2023_02_11_Time_249_ms_(100.00%)_Space_38.2_MB_(100.00%)

class Solution {
    fun findLongestChain(pairs: Array): Int {
        if (pairs.size == 1) {
            return 1
        }
        pairs.sortWith { a: IntArray, b: IntArray ->
            a[1] - b[1]
        }
        var min = pairs[0][1]
        var max = 1
        for (i in 1 until pairs.size) {
            if (pairs[i][0] > min) {
                max++
                min = pairs[i][1]
            }
        }
        return max
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy