본문 바로가기
Problem Solving/LeetCode

[LeetCode] 747. Largest Number At Least Twice of Others - Java

by graycode 2026. 10. 9.

• 문제 링크

 

Largest Number At Least Twice of Others - LeetCode

Can you solve this real interview question? Largest Number At Least Twice of Others - You are given an integer array nums where the largest integer is unique. Determine whether the largest element in the array is at least twice as much as every other numbe

leetcode.com

 

• 풀이 코드

public class Solution {

    public int dominantIndex(int[] nums) {
        int idx = 0;
        for (int i = 0; i < nums.length; i++) if (nums[idx] < nums[i]) idx = i;
        for (int i = 0; i < nums.length; i++) if (idx != i && nums[idx] < nums[i] * 2) return -1;

        return idx;
    }

}

댓글