var maxProduct = (nums) => {
const isEmpty = nums.length === 0;
if (isEmpty) return 0;
return linearSearch(nums);
};
const linearSearch = (nums, max = nums[0]) => {
for (let index = 0; index < nums.length; index++) {
max = getMax(nums, index, max);
}
return max;
};
const getMax = (nums, index, max, product = 1) => {
for (let num = index; num < nums.length; num++) {
product *= nums[num];
max = Math.max(max, product);
}
return max;
};
var maxProduct = (nums) => {
const isEmpty = nums.length === 0;
if (isEmpty) return 0;
return greedySearch(nums);
};
const greedySearch = (nums) => {
let min = (max = product = nums[0]);
for (let num = 1; num < nums.length; num++) {
const [minProduct, maxProduct] = [min * nums[num], max * nums[num]];
min = Math.min(maxProduct, minProduct, nums[num]);
max = Math.max(maxProduct, minProduct, nums[num]);
product = Math.max(product, max);
}
return product;
};