C++ One Pass


  • 0
    class Solution {
    public:
        int dominantIndex(vector<int>& nums) {
            if (nums.size() < 2) return -1;
            int first = 0, second = -1;
            for (int i = 1; i < nums.size(); i++) {
                if (nums[i] > nums[first]) {
                    second = first;
                    first = i;
                }
                else if (second == -1 || nums[i] > nums[second]) second = i;
            }
    
            return nums[first] >= nums[second] * 2 ? first : -1; 
        }
    };
    

Log in to reply
 

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