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

g0101_0200.s0128_longest_consecutive_sequence.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0101_0200.s0128_longest_consecutive_sequence

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Union_Find
// #Big_O_Time_O(N_log_N)_Space_O(1) #2022_09_03_Time_460_ms_(97.77%)_Space_51_MB_(99.32%)

import java.util.PriorityQueue

class Solution {
    fun longestConsecutive(nums: IntArray): Int {
        if (nums.isEmpty()) {
            return 0
        }
        val queue = PriorityQueue()
        nums.forEach {
            queue.add(it)
        }
        var lastNum = Integer.MIN_VALUE
        var length = 0
        var maxLength = 1
        while (queue.isNotEmpty()) {
            val num = queue.poll()
            if (num == lastNum) {
                continue
            }
            length ++
            if (num - lastNum > 1) {
                length = 1
            }
            lastNum = num
            maxLength = maxOf(maxLength, length)
        }
        return maxLength
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy