Largest Number At Least Twice of Others

2018/1/20 posted in  leetcode

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