#152
Maximum Product Subarray
medium· 1-D DPruns: 0Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product. The test cases are generated so the answer fits in a 32-bit integer.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 354
class Solution:
def maxProduct(self, nums: list[int]) -> int:
result = max(nums)
curr_max = curr_min = 1
for num in nums:
tmp = curr_max * num
curr_max = max(num, tmp, curr_min * num)
curr_min = min(num, tmp, curr_min * num)
result = max(result, curr_max)
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.