Just largest value, one pass, O(N) T, O(1) S


  • 0
    M
    int dominantIndex(vector<int>& nums) {
            
            int max_num = nums[0];
            int idx = 0;
            
            for(int i = 1; i < nums.size(); i++)
            {
                if(max_num > nums[i])
                {
                    if(2 * nums[i] > max_num)
                    {
                        idx = -1;
                    }
                }else{
                    if(2 * max_num > nums[i])
                    {
                        max_num = nums[i];
                        idx = -1;
                    }else{
                        max_num = nums[i];
                        idx = i;
                    }
                }
            }
            
            return idx;
            
        }
    

Log in to reply
 

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