当前位置:网站首页>1913. 两个数对之间的最大乘积差-无需排序法
1913. 两个数对之间的最大乘积差-无需排序法
2022-07-25 23:21:00 【Mr Gao】
1913. 两个数对之间的最大乘积差-无需排序法
两个数对 (a, b) 和 (c, d) 之间的 乘积差 定义为 (a * b) - (c * d) 。
例如,(5, 6) 和 (2, 7) 之间的乘积差是 (5 * 6) - (2 * 7) = 16 。
给你一个整数数组 nums ,选出四个 不同的 下标 w、x、y 和 z ,使数对 (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
看到这个题目,很多人第一反应可能是排序,但其实,如果用排序那就慢了,我们可以不需要排序,也可以可以找到最大的两个数和最小的两个数,解题代码如下:
int maxProductDifference(int* nums, int numsSize){
int min1=0,max1=0;
int min2=1,max2=1;
for(int i=2;i<numsSize;i++){
if(nums[i]>nums[max1]&&nums[i]>nums[max2]){
if(nums[max1]>nums[max2]){
max2=i;
}
else{
max1=i;
}
}
else if(nums[i]>nums[max1]&&nums[i]<=nums[max2]){
max1=i;
}
else if(nums[i]<=nums[max1]&&nums[i]>nums[max2]){
max2=i;
}
if(nums[i]<nums[min1]&&nums[i]<nums[min2]){
if(nums[min1]>nums[min2]){
min1=i;
}
else{
min2=i;
}
}
else if(nums[i]<nums[min1]&&nums[i]>=nums[min2]){
min1=i;
}
else if(nums[i]>=nums[min1]&&nums[i]<nums[min2]){
min2=i;
}
}
printf ("mx %d %d %d %d",min1,min2,max1,max2);
return nums[max1]*nums[max2]-nums[min1]*nums[min2];
}
边栏推荐
- Implementation of date class
- PCL basic operation Encyclopedia
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
- Which securities firm is the best and safest for beginners to open an account
- WebMvcConfigurationSupport
- npm+模块加载机制
- 电商RPA,大促轻松上阵的法宝
- Mongodb update operator (modifier)
- Classes and objects (3)
- Node基础
猜你喜欢

The VM session was closed before any attempt to power it on

The small icon of notification setting shows a small square

XxE & XML external entity injection utilization and bypass

Multimodal deep multi modal sets

Details of notification (status bar notification)
![[interface performance optimization] reasons for index failure and how to optimize SQL](/img/b9/64058c823c4497ac36bfb62a101816.jpg)
[interface performance optimization] reasons for index failure and how to optimize SQL

Mongodb features, differences with MySQL, and application scenarios

What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?

多模态——Deep Multi-Modal Sets

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
随机推荐
Analysis of direction finding error of multi baseline interferometer system
Node基础
The VM session was closed before any attempt to power it on
Wamp MySQL empty password
Ffmpeg first learning (only for coding)
@Import
How does PHP remove an element from an array based on the key value
类和对象(3)
Recommended system - an embedded learning framework for numerical features in CTR prediction
idea设置get、set模板解决boolean类型字段的命名问题
Rendering, filtering (filtering) and sorting of lists
Data filtering of MATLAB
Firewall command simple operation
Network Security Learning notes-1 file upload
CTS测试方法「建议收藏」
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
学习探索-3d轮播卡片
[interface performance optimization] reasons for index failure and how to optimize SQL
TS basic data type
r语言绘图参数(R语言plot画图)