LEETCODE | PYTHON | 1464 | 数组中两元素的最大乘积
1. 题目
给你一个整数数组 nums,请你选择数组的两个不同下标 i 和 j,使 (nums[i]-1)*(nums[j]-1) 取得最大值。
请你计算并返回该式的最大值。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 代码
class Solution:
def maxProduct(self, nums: List[int]) -> int:
#nums = [10,2,5,2]
#初始化
Max1 = 0
Max2 = 0
#遍历找到最大和第二大的数值
for i in range(len(nums)):
if nums[i] >= Max1:
Max2 = Max1
Max1 = nums[i]
if nums[i]!=Max1 and nums[i]>Max2:
Max2 = nums[i]
#print(Max1,Max2)
return (Max1-1)*(Max2-1)