Sign of the Product of an Array

HS K·2023년 5월 1일

문제설명

There is a function signFunc(x) that returns:

  • 1 if x is positive.
  • 1 if x is negative.
  • 0 if x is equal to 0.

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

제한사항

  • 1 <= nums.length <= 1000
  • 100 <= nums[i] <= 100

입출력 예

Example 1:

Input: nums = [-1,-2,-3,-4,3,2,1]
Output: 1
Explanation: The product of all values in the array is 144, and signFunc(144) = 1

Example 2:

Input: nums = [1,5,0,2,-3]
Output: 0
Explanation: The product of all values in the array is 0, and signFunc(0) = 0

Example 3:

Input: nums = [-1,1,-1,1,-1]
Output: -1
Explanation: The product of all values in the array is -1, and signFunc(-1) = -1

내가 쓴 답

/**
 * @param {number[]} nums
 * @return {number}
 */
var arraySign = function(nums) {
    let total = 1;
    for(i=0; i<nums.length; i++) {
        total *= nums[i]
    }
    if(total>0) {
        return 1
    }
    else if(total === 0) {
        return 0
    }
    else if(total<0) {
        return -1
    }
};

하지만 undefined 가 떴다.

[1,28,-91,-62,-36,-1,-84,-90,-92,61,6,-58,-60,2,51,-15,-18,-81,87,84,100,-84,-13,-87,-33,72,-72,-59,-79,28,-69,-97,-93,17,67,11,-12,19,5,42,-85,71,-77,-82,26,-58,-51,-14,63,-85,-86,66,47,57,-86,-25,-75,59,-17,-71,89,-78,-42,30,39,61,-96,-30,-29,-92,-90,69,20,65,32,92,44,-71,-18,87,-86,23,-96,-21,-49,-96,-98,35,-11,-1,81,-48,68,5,75,57,-30,-7,32,86,-29,-86,-61,45,-31,-93,-26,-9,86,57,-52,75,68,-58,14,27,-94,47,-38,-44,75,-83,21,-83,43,62,74,97,78,43,54,28,64,-19,-89,88,68,18,-96,-83,-25,-71,88,-84,-24,-61,72,-90,-56,29,46,56,51,16,66,-2,65,-95,16,51,42,61,99,89,-93,59,-99,69,26,-61,21,41,40,-4,-49,3,-96,57,65,72,-41,-77,-4,61,71,-88,21,-95,38,64,92,0,-63]

왜냐하면 이렇게 많은 요소들을 곱하게되면 JavaScript에서 숫자의 범위에서 벗어나기 때문에 무한대로 되어버린다.

var arraySign = function(nums) {
    let total = 1;
    for(let i=0; i<nums.length; i++) {
        let sign = Math.sign(nums[i]);
        if(sign === 0) {
            return 0;
        }
        total *= sign;
    }
    return total;
};

다른풀이

/**
 * @param {number[]} nums
 * @return {number}
 */
var arraySign = function(nums) {
    let total = 1;
    for(let i=0; i<nums.length; i++) {
        let sign = Math.sign(nums[i]);
        if(sign === 0) {
            return 0;
        }
        total *= sign;
    }
    return total;
};

속도 백분위

피드백

굉장히 쉬운 문제였지만 허를 찌르는 문제였다. 논리적으로 맞는데 틀렸다고하니 도대체 어디가 문제인지 감을 못잡았었다.

profile
주의사항 : 최대한 정확하게 작성하려고 하지만, 틀릴내용이 있을 수도 있으니 유의!

0개의 댓글