Skip to content

Commit 231235b

Browse files
authored
Create maximum-product-of-two-elements-in-an-array.py
1 parent 800a530 commit 231235b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxProduct(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
m1 = m2 = 0
11+
for num in nums:
12+
if num > m1:
13+
m1, m2 = num, m1
14+
elif num > m2:
15+
m2 = num
16+
return (m1-1)*(m2-1)

0 commit comments

Comments
 (0)