263. MaxProductOfThree

아현·2021년 8월 15일
0

Algorithm

목록 보기
275/400



1. JavaScript


정해

// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    let left, right, triplet;
    
    A.sort((a, b) => a - b);
    N = A.length;
    if(A[N-1] <= 0) {
        triplet = A[N-3] * A[N-2] * A[N-1];
    }else {
        left = A[0] * A[1] * A[N-1];
        right = A[N-3] * A[N-2] * A[N-1];
        if(left > right) {
            triplet = left;
        }else {
            triplet = right;
        }
    }
    
    return triplet;
}



  • 음수 * 음수 고려해주기



44%



// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    A.sort((a, b) => b - a)
    return A.slice(0, 3).reduce((a, b) => {return a * b}, 1)

}



2. Python


# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    A.sort()
    return max(A[0] * A[1] * A[-1], A[-3] * A[-2] * A[-1])

profile
For the sake of someone who studies computer science

0개의 댓글