luoheng
12/24/2019 - 7:37 AM

dominantIndex

func dominantIndex(nums []int) int {
    max, maxi, max2 := 0, 0, 0
    for i, n := range nums {
        if n > max {
            max, maxi, max2 = n, i, max
        } else if n < max && n > max2 {
            max2 = n
        }
    }
    if max >= 2 * max2 {
        return maxi
    }
    return -1
}