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

g0401_0500.s0414_third_maximum_number.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0401_0500.s0414_third_maximum_number

// #Easy #Array #Sorting #2022_12_06_Time_317_ms_(73.00%)_Space_36.9_MB_(94.00%)

class Solution {
    fun thirdMax(nums: IntArray): Int {
        var max1 = Long.MIN_VALUE
        var max2 = Long.MIN_VALUE
        var max3 = Long.MIN_VALUE
        for (i in nums) {
            max1 = Math.max(max1, i.toLong())
        }
        for (i in nums) {
            if (i.toLong() == max1) {
                continue
            }
            max2 = Math.max(max2, i.toLong())
        }
        for (i in nums) {
            if (i.toLong() == max1 || i.toLong() == max2) {
                continue
            }
            max3 = Math.max(max3, i.toLong())
        }
        return (if (max3 == Long.MIN_VALUE) max1 else max3).toInt()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy