Skip to content

Commit e6db2ca

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 40.50 MB (Top 45.54%)
1 parent c1976bf commit e6db2ca

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 40.50 MB (Top 45.54%)
2+
13
class Solution {
24
public int dominantIndex(int[] nums) {
3-
int max = -1;
4-
int smax = -1;
5-
6-
for(int i=0;i<nums.length;i++){
7-
8-
if(max == -1 || nums[i] >= nums[max]){
9-
smax = max;
10-
max = i;
11-
}else if(smax == -1 || nums[i] >= nums[smax]){
12-
smax = i;
13-
}
5+
if(nums == null || nums.length == 0){
6+
return -1;
147
}
158

16-
17-
if(nums[max] >= 2*nums[smax]){
18-
return max;
9+
if(nums.length == 1){
10+
return 0;
1911
}
12+
int max = Integer.MIN_VALUE + 1;
13+
int secondMax = Integer.MIN_VALUE;
14+
int index = 0;
2015

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

0 commit comments

Comments
 (0)