Back to Leetcode Go

152. Maximum Product Subarray

website/content/ChapterFour/0100~0199/0152.Maximum-Product-Subarray.md

1.7.11.8 KB
Original Source

152. Maximum Product Subarray

题目

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.

Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

题目大意

给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。

解题思路

  • 给出一个数组,要求找出这个数组中连续元素乘积最大的值。
  • 这一题是 DP 的题,状态转移方程是:最大值是 Max(f(n)) = Max( Max(f(n-1)) * n, Min(f(n-1)) * n);最小值是 Min(f(n)) = Min( Max(f(n-1)) * n, Min(f(n-1)) * n)。只要动态维护这两个值,如果最后一个数是负数,最大值就在负数 * 最小值中产生,如果最后一个数是正数,最大值就在正数 * 最大值中产生。

代码

go

package leetcode

func maxProduct(nums []int) int {
	minimum, maximum, res := nums[0], nums[0], nums[0]
	for i := 1; i < len(nums); i++ {
		if nums[i] < 0 {
			maximum, minimum = minimum, maximum
		}
		maximum = max(nums[i], maximum*nums[i])
		minimum = min(nums[i], minimum*nums[i])
		res = max(res, maximum)
	}
	return res
}


<div style="display: flex;justify-content: space-between;align-items: center;"> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0151.Reverse-Words-in-a-String/">⬅️上一页</a></p> <p><a href="https://books.halfrost.com/leetcode/ChapterFour/0100~0199/0153.Find-Minimum-in-Rotated-Sorted-Array/">下一页➡️</a></p> </div>