[JavaScript] LeetCode : Largest Number At Least Twice of Others
728x90

๋ฌธ์ œ

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 number in the array. If it is, return the index of the largest element, or return -1 otherwise.

์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์˜ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๋‚ด ๋‹ค๋ฅธ ์ˆ˜์˜ ์ตœ์†Œ ๋‘ ๋ฐฐ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ ,
์กฐ๊ฑด์— ์ถฉ์กฑ๋œ๋‹ค๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.


ํ’€์ด

var dominantIndex = function(nums) {
    if (nums.length === 1) return 0;

    let maxNumber = Math.max(...nums);

    let index = 0;
    for (let i = 0; i < nums.length; i++) {
        if (maxNumber === nums[i]) {
            index = i;
            continue;
        }

        if (maxNumber < nums[i] * 2) {
            return -1;
        }
    }

    return index;
};


์ƒ๊ฐํ•œ ํ๋ฆ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  • for๋ฌธ์„ ๋Œ๋ฉด์„œ ํฐ ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ์ˆ˜์˜ 2๋ฐฐ๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธํ•œ๋‹ค.
  • ์ถฉ์กฑํ•˜๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋จผ์ € [1]์ผ๋•Œ ๊ฒฐ๊ณผ๊ฐ€ 0์ด ๋‚˜์˜ค๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์žˆ์–ด์„œ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ผ๋•Œ๋Š” ๋ฐ”๋กœ 0์œผ๋กœ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค. (์–ด์ฐจํ”ผ ๋น„๊ตํ•  ๋‹ค๋ฅธ ์š”์†Œ๊ฐ€ ์—†์œผ๋‹ˆ)
๊ทธ๋ฆฌ๊ณ  for๋ฌธ์„ ํ†ตํ•ด์„œ๋„ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, Max ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด๋ณด์•˜๋‹ค.
์ด์–ด์„œ for๋ฌธ์„ ํ†ตํ•ด ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ์ˆ˜์˜ ๋‘ ๋ฐฐ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
๊ฐ€์žฅ ํฐ ์ˆ˜์™€ ๊ฐ™์€ ์ธ๋ฑ์Šค ๊ฐ’์ผ ๊ฒฝ์šฐ์—๋Š” ๋„˜์–ด๊ฐ€๋„๋ก ํ–ˆ๊ณ ,
๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋‘ ๋ฐฐ๋œ ์ˆ˜๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ์—๋Š” -1์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋‹ค.


๋‹ค๋ฅธ ํ’€์ด

LeetCode์—์„œ ๋ณธ ๋‹ค๋ฅธ ํ’€์ด.
๋‚ด๊ฐ€ ์‚ฌ์šฉํ–ˆ๋˜ if๋ฌธ๊ณผ ๊ฑฐ์˜ ๋™์ผํ•œ ์กฐ๊ฑด์ด๋‹ค.
์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์ง€ ์•Š๊ณ  ๋‘ ๋ฐฐ๋œ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ณด๋‹ค ํด ๋•Œ -1์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋‹ค.

var dominantIndex = function(nums) {
    let ans = -1;

    let mx = Math.max(...nums)

    for(let i = 0; i < nums.length; ++i) {
        if(nums[i] !== mx && (nums[i] * 2) > mx) 
            return -1

        if(mx === nums[i]) ans = i
    } 

    return ans
};


์•„๋ž˜์—์„œ ์›๋ฌธ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/603801/JavaScript-Solution






๐Ÿคธโ€โ™€๏ธ๐Ÿคธโ€โ™€๏ธ๐Ÿคธโ€โ™€๏ธ๐Ÿคธโ€โ™€๏ธ๐Ÿคธโ€โ™€๏ธ๐Ÿคธโ€โ™€๏ธ
๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™Œ
ํฌ์ŠคํŒ…๋“ค์€ ๊ณต๋ถ€์ค‘์ธ ๋‚ด์šฉ์„ ๊ธ€๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋ผ ๋ถ€์กฑํ•œ ์ ์ด ๋งŽ์œผ๋‹ˆ ์ฐธ๊ณ  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
๋ถ€์กฑํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ฝ”๋ฉ˜ํŠธ๋Š” ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค.
์ข‹์€ ํ•˜๋ฃจ ๋˜์„ธ์š”, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™‚

728x90

'๐Ÿ““ Algorithm > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] leetCode : Linked List Cycle II  (3) 2022.05.03
[JavaScript] leetCode : Valid Mountain Array  (3) 2022.05.02
[JavaScript] LeetCode : Add Binary  (0) 2022.04.22
[JavaScript] LeetCode : Plus One  (0) 2022.04.21
[JavaScript] LeetCode : Find Pivot Index  (1) 2022.04.20