https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/
题目:判断最大数是否最少是次大数的2倍。
思路:直接求出最大值,次大值判断即可。
class Solution {
public:
int dominantIndex(vector<int>& nums) {
if(nums.size()<=1) return 0;
int max1 = -1,max2 = -1,index = 0;
for(int x=0;x<nums.size();x++){
if(nums[x]>max1) max2=max1,max1 = nums[x],index =x;
else if(nums[x]>max2) max2=nums[x];
}
if(max1>=2*max2) return index;
return -1;
}
};