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배값이 최대값보다 커지면 바로 끝내버려야 함
'JAVA > leetcode' 카테고리의 다른 글
[LeetCode Medium] Spiral Matrix !! - BFS (0) | 2021.08.06 |
---|---|
[LeetCode] Array - Plus One (0) | 2021.08.06 |
[LeetCode] Array - Find Pivot Index !! (0) | 2021.08.06 |
[LeetCode] Array - Find All Numbers Disappeared in an Array (0) | 2021.08.06 |
[LeetCode Medium] Array - Container With Most Water !! - Two-pointer (0) | 2021.08.06 |