题解 | #连续子数组的最大乘积#
连续子数组的最大乘积
http://www.nowcoder.com/practice/fd8c819c07c9493887bfac8549c119f4
分享一个JavaScript的实现
let n = ~~readline()
let arr = readline().split(' ').map(x => ~~x)
function main(arr, n) {
let pre_max = arr[0], pre_min = arr[0]
let ans = arr[0]
for(let i = 1 ; i < n ; i++) {
let cur_max = Math.max(pre_max * arr[i], pre_min * arr[i], arr[i])
let cur_min = Math.min(pre_max * arr[i], pre_min * arr[i], arr[i])
ans = Math.max(ans, cur_max)
pre_max = cur_max
pre_min = cur_min
}
return ans
}
console.log(main(arr, n))