414.Third Maximum Number


Given anon-emptyarray of integers, return thethirdmaximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Example 1:

Input:
[3, 2, 1]

Output:
1

Explanation:
The third maximum is 1.

Example 2:

Input:
[1, 2]

Output:
2

Explanation:
The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:

Input:
[2, 2, 3, 1]

Output:
1

Explanation:
Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.

Solutions

  • golang
func thirdMax(nums []int) int {
    save := [] int {int(math.Inf(-1)),int(math.Inf(-1)),int(math.Inf(-1))}
    for _,v := range nums {
        if v != save[0] && v != save[1] && v != save[2]{
            if v > save[0]{
                save = [] int {v,save[0],save[1]}
            }else if v > save[1]{
                save = [] int {save[0],v,save[1]}
            }else if v > save[2]{
                save = [] int {save[0],save[1],v}
            }
        }
    }
    if save[2] == int(math.Inf(-1)){
        return save[0]
    }
    return save[2]
}
  • scala
object Solution {
    def thirdMax(nums: Array[Int]): Int = {
        val n = nums.distinct.sorted.reverse
        if(n.length < 3)
            return n(0)
        return n(2)
    }
}

results matching ""

    No results matching ""