Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ecb5740

Browse files
create Solution.py
1 parent 766d0b6 commit ecb5740

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Beats 88% in python.
3+
class Solution(object):
4+
def maxProduct(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
if not nums:
10+
return 0
11+
localMin = localMax = maxi = nums[0]
12+
13+
for i in range(1, len(nums)):
14+
if nums[i] < 0:
15+
localMin, localMax = localMax, localMin
16+
17+
localMin = min(nums[i], localMin * nums[i])
18+
localMax = max(nums[i], localMax * nums[i])
19+
maxi = max(localMax, maxi)
20+
21+
return maxi

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /