Question:
Given an integer array, find three numbers whose product is maximum and output the maximum product.
给定整数数组,找出其最大值并输出最大乘积的三个数字。
Example:
Input: [1,2,3]
Output: 6
Input: [1,2,3,4]
Output: 24
Solution:
/**
* @param {number[]} nums
* @return {number}
*/
//此题需要考虑负数,比较最大三个数和最大数乘最小2个数的乘积
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]
};