O(N) one pass golang solution


  • 0
    G
    func dominantIndex(nums []int) int {
        if len(nums) == 1{
            return 0
        }
        
        min := math.MinInt32
        max := nums[0]
        midx := 0
        
        for i,v := range nums[1:]{
            if v > max{
                min = max
                max = v
                midx = i + 1
            }else if(v > min){
                min = v
            }    
        }
        
        if max >= min*2 || min == 0 {
            return midx
        }
        
        return -1
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.