0%

maxProduct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums: number[]): number {
let max = nums[0];
let min = nums[0];
let res = nums[0];

for (let i = 1; i < nums.length; i++) {
let tmp = min;
min = Math.min(nums[i], Math.min(max * nums[i], min * nums[i]));
max = Math.max(nums[i], Math.max(max * nums[i], tmp * nums[i]));
res = Math.max(res, max);
}
return res;
};