-
Notifications
You must be signed in to change notification settings - Fork 612
/
152.py
39 lines (30 loc) · 1.01 KB
/
152.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
32
33
34
35
36
37
38
39
'''
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
'''
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
max_so_far, min_so_far, result = nums[0], nums[0], nums[0]
for index in range(1, len(nums)):
if nums[index] > 0:
max_so_far = max(max_so_far*nums[index], nums[index])
min_so_far = min(min_so_far*nums[index], nums[index])
else:
temp = max_so_far
max_so_far = max(min_so_far*nums[index], nums[index])
min_so_far = min(temp*nums[index], nums[index])
result = max(result, max_so_far)
return result