1464. Maximum Product of Two Elements in an Array

Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).

很简单,就是找出最大值和次大值相乘。

class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max_value = 0
        next_max_value = 0
        for num in nums:
            if num > max_value:
                next_max_value = max_value
                max_value = num
            else:
                if num > next_max_value:
                    next_max_value = num
        return (max_value - 1) * (next_max_value - 1)

 

posted @ 2020-06-28 23:07  whatyouthink  阅读(63)  评论(0编辑  收藏  举报