Skip to content

Commit

Permalink
Create: 152-Maximum-Product-Subarray.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 17, 2022
1 parent be1949e commit b117539
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions go/152-Maximum-Product-Subarray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func maxProduct(nums []int) int {
res, curMin, curMax := nums[0], 1, 1

for i := 0; i < len(nums); i++ {
temp := curMax * nums[i]
curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i])
curMin = min(min(temp, nums[i] * curMin), nums[i])
res = max(res, curMax)
}
return res
}

// Golang does not have a built-in max for integers
func max(a int, b int) int {
if (a > b) {
return a
}
return b
}

// Golang does not have a built-in min for integers
func min(a int, b int) int {
if (a < b) {
return a
}
return b
}

0 comments on commit b117539

Please sign in to comment.