NOTE
Maximum Product Subarray
Given an integer array nums, find the contiguous subarray with the largest product (the subarray contains at least one number) and return that product.
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
- 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
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub