NOTE

Maximum Product Subarray

LeetCode notes on the Maximum Product Subarray problem.

Data Structures & AlgorithmsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Problem Description

Given an integer array nums, find the contiguous subarray with the largest product (the subarray contains at least one number) and return that product.

2. Approach

  1. Approach 1
    • Brute force
    • Use two nested loops to compute the maximum product

3. Implementation

3.1. Brute Force

func maxProduct(nums []int) int {
	max := math.MinInt32
	for i := 0; i < len(nums); i++ {
		res := 1
		for j := i; j < len(nums); j++ {
			res *= nums[j]
			max = Max(max, res)
		}
	}

	return max
}

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub