当前位置:网站首页>1913. Maximum product difference between two number pairs - no sorting required
1913. Maximum product difference between two number pairs - no sorting required
2022-07-25 23:25:00 【Mr Gao】
1913. The maximum product difference between two number pairs - No sorting
Two pairs of numbers (a, b) and (c, d) Between The product difference Defined as (a * b) - (c * d) .
for example ,(5, 6) and (2, 7) The product difference between is (5 * 6) - (2 * 7) = 16 .
Give you an array of integers nums , Choose four Different Subscript w、x、y and z , Make number pair (nums[w], nums[x]) and (nums[y], nums[z]) Between The product difference Fetch Maximum .
Returns... Of the product differences obtained in this way Maximum .
Example 1:
Input :nums = [5,6,2,7,4]
Output :34
explain : You can choose the subscript as 1 and 3 The elements of form the first number pair (6, 7) And subscripts 2 and 4 Make up the second number 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
explain : You can choose the subscript as 3 and 6 The elements of form the first number pair (9, 8) And subscripts 1 and 5 Make up the second number pair (2, 4)
The product difference is (9 * 8) - (2 * 4) = 64
See this topic , The first reaction of many people may be sorting , But in fact , If you use sorting, it will be slow , We don't need to sort , You can also find the two largest numbers and the two smallest numbers , The solution code is as follows :
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];
}
边栏推荐
- Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
- Flight control implementation of four rotor aircraft "suggestions collection"
- npm+模块加载机制
- Source code of YY music wechat applet imitating Netease cloud music
- Solve the problem phpstudy failed to import the database
- Redis过期键的删除策略[通俗易懂]
- Vscode shortcut key: collapse and expand code
- 新手哪个券商开户最好 开户最安全
- Mongodb features, differences with MySQL, and application scenarios
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
猜你喜欢

Node基础

多模态——Deep Multi-Modal Sets

POI特效 市场调研

XXE&XML-外部实体注入-利用和绕过

Very simple vsplayaudio online music player plug-in

Mongodb features, differences with MySQL, and application scenarios

How does Navicat modify the language (Chinese or English)?

idea设置get、set模板解决boolean类型字段的命名问题

The small icon of notification setting shows a small square

Expression of directional signal -- complex exponential signal
随机推荐
WebMvcConfigurationSupport
Expression of directional signal -- complex exponential signal
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
动态内存管理
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
PHP wechat scan code, follow official account and authorize login source code
Serialize addition, deletion, modification and query
ETL tool (data synchronization) II
ETL工具(数据同步) 二
Unity 使用宏
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
Node Foundation
POI special effects Market Research
数组中重复的数字
Overview of MES system equipment management (Part 2)
@Import
npm+模块加载机制
Implementation of date class
CSV generated by PHP cannot completely display the number with leading 0