加载中...
1913-两个数对之间的最大乘积差(Maximum Product Difference Between Two Pairs)
发表于:2021-12-03 | 分类: 简单
字数统计: 523 | 阅读时长: 2分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/maximum-product-difference-between-two-pairs

英文原文

The product difference between two pairs (a, b) and (c, d) is defined as (a * b) - (c * d).

  • For example, the product difference between (5, 6) and (2, 7) is (5 * 6) - (2 * 7) = 16.

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.

Return the maximum such product difference.

 

Example 1:

Input: nums = [5,6,2,7,4]
Output: 34
Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) - (2 * 4) = 34.

Example 2:

Input: nums = [4,2,5,9,7,4,8]
Output: 64
Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
The product difference is (9 * 8) - (2 * 4) = 64.

 

Constraints:

  • 4 <= nums.length <= 104
  • 1 <= nums[i] <= 104

中文题目

两个数对 (a, b)(c, d) 之间的 乘积差 定义为 (a * b) - (c * d)

  • 例如,(5, 6)(2, 7) 之间的乘积差是 (5 * 6) - (2 * 7) = 16

给你一个整数数组 nums ,选出四个 不同的 下标 wxyz ,使数对 (nums[w], nums[x])(nums[y], nums[z]) 之间的 乘积差 取到 最大值

返回以这种方式取得的乘积差中的 最大值

 

示例 1:

输入:nums = [5,6,2,7,4]
输出:34
解释:可以选出下标为 1 和 3 的元素构成第一个数对 (6, 7) 以及下标 2 和 4 构成第二个数对 (2, 4)
乘积差是 (6 * 7) - (2 * 4) = 34

示例 2:

输入:nums = [4,2,5,9,7,4,8]
输出:64
解释:可以选出下标为 3 和 6 的元素构成第一个数对 (9, 8) 以及下标 1 和 5 构成第二个数对 (2, 4)
乘积差是 (9 * 8) - (2 * 4) = 64

 

提示:

  • 4 <= nums.length <= 104
  • 1 <= nums[i] <= 104

通过代码

高赞题解

func maxProductDifference(a []int) int {
	sort.Ints(a)
	n := len(a)
	return a[n-1]*a[n-2] - a[0]*a[1]
}

统计信息

通过次数 提交次数 AC比率
9242 11106 83.2%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1928-规定时间内到达终点的最小花费(Minimum Cost to Reach Destination in Time)
下一篇:
1914-循环轮转矩阵(Cyclically Rotating a Grid)
本文目录
本文目录