MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / maxProduct

Function maxProduct

go/0152-maximum-product-subarray.go:1–11  ·  view source on GitHub ↗
(nums []int)

Source from the content-addressed store, hash-verified

1func maxProduct(nums []int) int {
2 res, curMin, curMax := nums[0], 1, 1
3
4 for i := 0; i < len(nums); i++ {
5 temp := curMax * nums[i]
6 curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i])
7 curMin = min(min(temp, nums[i] * curMin), nums[i])
8 res = max(res, curMax)
9 }
10 return res
11}
12
13// Golang does not have a built-in max for integers
14func max(a int, b int) int {

Callers

nothing calls this directly

Calls 2

maxFunction · 0.70
minFunction · 0.70

Tested by

no test coverage detected