Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created January 17, 2018 14:13
Show Gist options
  • Save s4553711/c5d2b30a14beaf1de04a4b16651ac3d1 to your computer and use it in GitHub Desktop.
Save s4553711/c5d2b30a14beaf1de04a4b16651ac3d1 to your computer and use it in GitHub Desktop.
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int maxi = -1, seci = -1;
for(int i = 0; i < nums.size(); i++) {
// cout << "b: i: " << i << " m: " << maxi << ", s: " << seci << endl;
if (nums[i] > nums[maxi]) {
int tmp = maxi;
maxi = i;
seci = tmp;
} else if (nums[i] > nums[seci]) {
seci = i;
}
// cout << "a: i: " << i << " m: " << maxi << ", s: " << seci << endl;
}
// cout << "maxi: " << maxi << endl;
// cout << "seci: " << seci << endl;
if (maxi == -1) return -1;
return nums[maxi] >= 2 * nums[seci] ? maxi : -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment