JAVA/leetcode
[LeetCode] Array - Largest Number At Least Twice of Others
JJunDol2
2021. 8. 6. 16:42
class Solution {
public int dominantIndex(int[] nums) {
if(nums.length == 1) return 0;
int result = -1;
int max = 0;
int maxIndex = 0;
for (int i = 0; i < nums.length; i++) {
if(max < nums[i]) {
max = nums[i];
maxIndex = i;
}
}
for (int i = 0; i < nums.length; i++) {
if(maxIndex == i)
continue;
if(max >= nums[i] * 2)
result = maxIndex;
else
return -1;
}
return result;
}
}
나머지 것들의 2배값이 최대값보다 커지면 바로 끝내버려야 함