[C++] 9ms one-pass solution, TC-O(n) and SC-O(1)


  • 0
    A

    class Solution {
    public:
    int dominantIndex(vector<int>& nums) {
    int max1 = INT_MIN, max2 = INT_MIN, i1 = -1;

    	for (int i = 0; i < nums.size(); ++i) {
    		if (max2 < nums[i]) { max2 = nums[i]; }
    
    		if (max1 < max2) {
    			int mt = max1;
    			max1 = max2;
    			max2 = mt;
    			
    			i1 = i;
    		}
    	}
    
    	if (2 * max2 > max1) { return -1; }
    	else { return i1; }
    }
    

    };


Log in to reply
 

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