C style one pass O(n)


  • 0
    K

    Here is the code:

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

Log in to reply
 

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