[leetcode] 1464. Maximum Product of Two Elements in an Array

网友投稿 304 2022-08-26

[leetcode] 1464. Maximum Product of Two Elements in an Array

Description

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).

Example 1:

Input: nums = [3,4,5,2]Output: 12 Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.

Example 2:

Input: nums = [1,5,4,5]Output: 16Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.

Example 3:

Input: nums = [3,7]Output: 12

Constraints:

2 <= nums.length <= 5001 <= nums[i] <= 10^3

分析

题目的意思是:给你一个数组,找出两个值的乘积最大的值,这道题如果用排序的话就很简单了,如果用冒泡排序就更简单了,我比较懒,一下子sort之后ac了,用冒泡排序遍历两次找到两个最大值,跳出直接返回结果也是可以的。

代码

class Solution: def maxProduct(self, nums: List[int]) -> int: nums.sort() return (nums[-1]-1)*(nums[-2]-1)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:两连败后8连胜,岳清爽说半决赛要死拼对手!
下一篇:[leetcode] 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
相关文章

 发表评论

暂时没有评论,来抢沙发吧~