forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.py
31 lines (25 loc) · 886 Bytes
/
Solution1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Source : https://leetcode.com/problems/maximum-product-subarray/
# Author : penpenps
# Time : 2019-07-21
from typing import List
# Travesal nums, use maxVal and minVal to keep max and min product end with current element
# Time Complexity: O(n)
# Space Complexity: O(1)
class Solution:
def maxProduct(self, nums: List[int]) -> int:
n = len(nums)
minVal, maxVal = nums[0], nums[0]
ans = nums[0]
for i in range(1, n):
# If negative number, it should swap min and max for sign switching
if nums[i] < 0:
minVal, maxVal = maxVal, minVal
maxVal = max(nums[i]*maxVal, nums[i])
minVal = min(nums[i]*minVal, nums[i])
ans = max(maxVal, ans)
return ans
if __name__ == '__main__':
s = Solution()
nums = [2,3,-2,4]
answer = s.maxProduct(nums)
print(answer)