nums
class Solution:
def maxProduct(self, nums: List[int]) -> int:
N = len(nums)
tmp = 1
maxi = tmp
for i in range(N):
tmp = nums[i]
maxi = max(maxi, tmp)
for j in range(i+1, N):
tmp *= nums[j]
maxi = max(maxi, tmp)
return maxi
아 타임리밋,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
class Solution:
def maxProduct(self, nums: List[int]) -> int:
product = set()
product.add(1)
maxi = nums[0]
for n in nums:
newProduct = set()
newProduct.add(n)
maxi = max(maxi, n)
for p in product:
newProduct.add(n*p)
maxi = max(maxi, n*p)
product = newProduct
return maxi
속도도 많이 느리다
class Solution:
def maxProduct(self, nums: List[int]) -> int:
N = len(nums)
minProduct = nums[0]
maxProduct = nums[0]
maxi = maxProduct
for i in range(1, N):
product = set()
product.add(minProduct*nums[i])
product.add(maxProduct*nums[i])
product.add(nums[i])
minProduct = min(product)
maxProduct = max(product)
maxi = max(maxi, maxProduct)
return maxi
그렇게 엄청 빠르진 않네