Largest Number At Least Twice of Others LeetCode solution in Java

1
class Solution {
2
public int dominantIndex(int[] nums) {
3
if (nums == null || nums.length == 0) {
4
return -1;
5
}
6

7
if (nums.length == 1) {
8
return 0;
9
}
10
int max = Integer.MIN_VALUE + 1;
11
int secondMax = Integer.MIN_VALUE;
12
int index = 0;
13

14
for (int i = 0; i < nums.length; i++) {
15
if (nums[i] > max) {
16
secondMax = max;
17
max = nums[i];
18
index = i;
19
} else if (nums[i] != max && nums[i] > secondMax) {
20
secondMax = nums[i];
21
}
22
}
23
if (secondMax * 2 <= max) {
24
return index;
25
}
26
return -1;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0