Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5772dc0

Browse files
747. Largest Number At Least Twice of Others
1 parent ff13f8c commit 5772dc0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 747. Largest Number At Least Twice of Others
3+
* https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/
4+
* @param {number[]} nums
5+
* @return {number}
6+
*/
7+
var dominantIndex = function (nums) {
8+
let max1 = 0;
9+
let max2 = 0;
10+
let maxIndex = 0;
11+
for (let i = 0; i < nums.length; i++) {
12+
if (nums[i] > max1) {
13+
max2 = max1;
14+
max1 = nums[i];
15+
maxIndex = i;
16+
} else if (nums[i] > max2) {
17+
max2 = nums[i];
18+
}
19+
}
20+
return max1 >= max2 * 2 ? maxIndex : -1;
21+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /