프로그래머스 최댓값 만들기(2)

JinYoungMo·2024년 5월 9일

정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소 중 두 개를 곱해 만들 수 있는 최댓값을 return하도록 solution 함수를 완성해주세요.

function solution(numbers) {
    numbers.sort((a, b) => a - b);
    const n = numbers.length;
    return Math.max(numbers[0] * numbers[1], numbers[n - 1] * numbers[n-2]);
}

오름 차순 정렬 후에 왼쪽이나 오른쪽에 가까울 수록 두 수의 곱이 최대가 되므로 이렇게 설계를 했습니다.

profile
blockchain core & payments and stable coins

0개의 댓글