Question:

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example:

  1. Input: [1,2,3]
  2. Output: 6
Input: [1,2,3,4]
Output: 24

Solution:

/**
 * @param {number[]} nums
 * @return {number}
 */
var maximumProduct = function(nums) {
    nums.sort((a,b)=>a-b)
    let len = nums.length
    return Math.max(nums[0]*nums[1], nums[len-3]*nums[len-2])*nums[len-1]
};

Runtime: 128 ms, faster than 17.82% of JavaScript online submissions for Maximum Product of Three Numbers.