aboutsummaryrefslogtreecommitdiff
path: root/max_product_two_elem.py
blob: 68ad11d2d85113a01edd90c1cf61aee863811ea7 (plain)
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
# 1464. Maximum Product of Two Elements in an Array
import math

"""
given the array of integers 'nums', you will choose two different indices 'i'
and 'j' of thatarray. return the maximum value of '(nums[i] - 1)  (nums[j] -
1)'
"""


class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max1 = max2 = -math.inf
        for n in nums:
            if n > max1:
                max2 = max1
                max1 = n
            elif n > max2:
                max2 = n
        return (max1 - 1) * (max2 - 1)


if __name__ == "__main__":
    obj = Solution()
    print(obj.maxProduct([3, 4, 5, 2]))
    print(obj.maxProduct([1, 5, 4, 5]))
    print(obj.maxProduct([3, 7]))