给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
测试用例的答案是一个 32-位 整数。
func maxProduct(nums []int) int {
length := len(nums)
if length == 1 {
return nums[0]
}
maxMul, minMul, res := nums[0], nums[0], nums[0]
for i := 1; i < length; i++ {
mx, mn := maxMul, minMul
maxMul = max(mx*nums[i], max(nums[i], mn*nums[i]))
minMul = min(mn*nums[i], min(nums[i], mx*nums[i]))
res = max(maxMul, res)
}
return res
}
func max(x, y int) int {
if x < y {
return y
}
return x
}
func min(x, y int) int {
if x < y {
return x
}
return y
}